Skip to content
Browse files

Removed backing field conventions.

Removed old WP7 stuff.
  • Loading branch information...
1 parent 12ffe77 commit 0a67c616426b653117b5ff165934aebdd520e96a @jlaanstra jlaanstra committed Mar 29, 2013
View
4 ReactiveUI.Tests/CommandBindingTests.cs
@@ -128,13 +128,13 @@ public class CommandBindViewModel : ReactiveObject
public ReactiveCommand _Command1;
public ReactiveCommand Command1 {
get { return _Command1; }
- set { this.RaiseAndSetIfChanged(x => x.Command1, value); }
+ set { this.RaiseAndSetIfChanged(ref _Command1, value); }
}
public ReactiveCommand _Command2;
public ReactiveCommand Command2 {
get { return _Command2; }
- set { this.RaiseAndSetIfChanged(x => x.Command2, value); }
+ set { this.RaiseAndSetIfChanged(ref _Command2, value); }
}
public CommandBindViewModel()
View
14 ReactiveUI.Tests/PropertyBindingTest.cs
@@ -22,45 +22,45 @@ public class PropertyBindViewModel : ReactiveObject
public string _Property1;
public string Property1 {
get { return _Property1; }
- set { this.RaiseAndSetIfChanged(x => x.Property1, value); }
+ set { this.RaiseAndSetIfChanged(ref _Property1, value); }
}
public int _Property2;
public int Property2 {
get { return _Property2; }
- set { this.RaiseAndSetIfChanged(x => x.Property2, value); }
+ set { this.RaiseAndSetIfChanged(ref _Property2, value); }
}
public double _JustADouble;
public double JustADouble {
get { return _JustADouble; }
- set { this.RaiseAndSetIfChanged(x => x.JustADouble, value); }
+ set { this.RaiseAndSetIfChanged(ref _JustADouble, value); }
}
public decimal _JustADecimal;
public decimal JustADecimal {
get { return _JustADecimal; }
- set { this.RaiseAndSetIfChanged(x => x.JustADecimal, value); }
+ set { this.RaiseAndSetIfChanged(ref _JustADecimal, value); }
}
public int _JustAInt32;
public int JustAInt32 {
get { return _JustAInt32; }
- set { this.RaiseAndSetIfChanged(x => x.JustAInt32, value); }
+ set { this.RaiseAndSetIfChanged(ref _JustAInt32, value); }
}
public double? _NullableDouble;
public double? NullableDouble {
get { return _NullableDouble; }
- set { this.RaiseAndSetIfChanged(x => x.NullableDouble, value); }
+ set { this.RaiseAndSetIfChanged(ref _NullableDouble, value); }
}
public ReactiveCollection<string> SomeCollectionOfStrings { get; protected set; }
public PropertyBindModel _Model;
public PropertyBindModel Model {
get { return _Model; }
- set { this.RaiseAndSetIfChanged(x => x.Model, value); }
+ set { this.RaiseAndSetIfChanged(ref _Model, value); }
}
public PropertyBindViewModel(PropertyBindModel model = null)
View
12 ReactiveUI.Tests/ReactiveNotifyPropertyChangedMixinTest.cs
@@ -34,19 +34,19 @@ public class HostTestFixture : ReactiveObject
public TestFixture _Child;
public TestFixture Child {
get { return _Child; }
- set { this.RaiseAndSetIfChanged(x => x.Child, value); }
+ set { this.RaiseAndSetIfChanged(ref _Child, value); }
}
public int _SomeOtherParam;
public int SomeOtherParam {
get { return _SomeOtherParam; }
- set { this.RaiseAndSetIfChanged(x => x.SomeOtherParam, value); }
+ set { this.RaiseAndSetIfChanged(ref _SomeOtherParam, value); }
}
public NonObservableTestFixture _PocoChild;
public NonObservableTestFixture PocoChild {
get { return _PocoChild; }
- set { this.RaiseAndSetIfChanged(x => x.PocoChild, value); }
+ set { this.RaiseAndSetIfChanged(ref _PocoChild, value); }
}
}
@@ -95,7 +95,7 @@ public class ObjChain1 : ReactiveObject
public ObjChain2 _Model = new ObjChain2();
public ObjChain2 Model {
get { return _Model; }
- set { this.RaiseAndSetIfChanged(x => x.Model, value); }
+ set { this.RaiseAndSetIfChanged(ref _Model, value); }
}
}
@@ -104,7 +104,7 @@ public class ObjChain2 : ReactiveObject
public ObjChain3 _Model = new ObjChain3();
public ObjChain3 Model {
get { return _Model; }
- set { this.RaiseAndSetIfChanged(x => x.Model, value); }
+ set { this.RaiseAndSetIfChanged(ref _Model, value); }
}
}
@@ -113,7 +113,7 @@ public class ObjChain3 : ReactiveObject
public HostTestFixture _Model = new HostTestFixture();
public HostTestFixture Model {
get { return _Model; }
- set { this.RaiseAndSetIfChanged(x => x.Model, value); }
+ set { this.RaiseAndSetIfChanged(ref _Model, value); }
}
}
View
8 ReactiveUI.Tests/ReactiveObjectTest.cs
@@ -15,31 +15,31 @@ public class TestFixture : ReactiveObject
[IgnoreDataMember]
public string IsNotNullString {
get { return _IsNotNullString; }
- set { this.RaiseAndSetIfChanged(x => x.IsNotNullString, ref _IsNotNullString, value); }
+ set { this.RaiseAndSetIfChanged(ref _IsNotNullString, value); }
}
[DataMember]
public string _IsOnlyOneWord;
[IgnoreDataMember]
public string IsOnlyOneWord {
get { return _IsOnlyOneWord; }
- set { this.RaiseAndSetIfChanged(x => x.IsOnlyOneWord, ref _IsOnlyOneWord, value); }
+ set { this.RaiseAndSetIfChanged(ref _IsOnlyOneWord, value); }
}
[DataMember]
public List<string> _StackOverflowTrigger;
[IgnoreDataMember]
public List<string> StackOverflowTrigger {
get { return _StackOverflowTrigger; }
- set { this.RaiseAndSetIfChanged(value.ToList()); }
+ set { this.RaiseAndSetIfChanged(ref _StackOverflowTrigger, value.ToList()); }
}
[DataMember]
public string _UsesExprRaiseSet;
[IgnoreDataMember]
public string UsesExprRaiseSet {
get { return _UsesExprRaiseSet; }
- set { this.RaiseAndSetIfChanged(x => x.UsesExprRaiseSet, ref _UsesExprRaiseSet, value); }
+ set { this.RaiseAndSetIfChanged(ref _UsesExprRaiseSet, value); }
}
[DataMember]
View
6 ReactiveUI.Tests/ReactiveValidatedObjectTest.cs
@@ -19,21 +19,21 @@ public ValidatedTestFixture()
[Required]
public string IsNotNullString {
get { return _IsNotNullString; }
- set { this.RaiseAndSetIfChanged(x => x.IsNotNullString, value); }
+ set { this.RaiseAndSetIfChanged(ref _IsNotNullString, value); }
}
public string _IsOnlyOneWord;
[Required]
[RegularExpression(@"^[a-zA-Z]+$")]
public string IsOnlyOneWord {
get { return _IsOnlyOneWord; }
- set { this.RaiseAndSetIfChanged(x => x.IsOnlyOneWord, value); }
+ set { this.RaiseAndSetIfChanged(ref _IsOnlyOneWord, value); }
}
public string _UsesExprRaiseSet;
public string UsesExprRaiseSet {
get { return _UsesExprRaiseSet; }
- set { _UsesExprRaiseSet = this.RaiseAndSetIfChanged(x => x.UsesExprRaiseSet, value); }
+ set { _UsesExprRaiseSet = this.RaiseAndSetIfChanged(ref _UsesExprRaiseSet, value); }
}
}
View
2 ReactiveUI.Tests/RoutingState.cs
@@ -11,7 +11,7 @@ public class TestViewModel : ReactiveObject, IRoutableViewModel
string _SomeProp;
public string SomeProp {
get { return _SomeProp; }
- set { this.RaiseAndSetIfChanged(x => x.SomeProp, value); }
+ set { this.RaiseAndSetIfChanged(ref _SomeProp, value); }
}
public string UrlPathSegment {
View
2 ReactiveUI.Xaml/Errors.cs
@@ -126,7 +126,7 @@ public class UserError : ReactiveObject
/// </summary>
public ReactiveCollection<IRecoveryCommand> RecoveryOptions {
get { return _RecoveryOptions; }
- protected set { this.RaiseAndSetIfChanged(x => x.RecoveryOptions, ref _RecoveryOptions, value); }
+ protected set { this.RaiseAndSetIfChanged(ref _RecoveryOptions, value); }
}
/// <summary>
View
13 ReactiveUI/CompatMixins.cs
@@ -24,18 +24,5 @@ public static IObservable<T> PermaRef<T>(this IConnectableObservable<T> This)
This.Connect();
return This;
}
-
-#if WP7
- public static bool HasFlag(this Enum This, Enum value)
- {
- // check if from the same type.
- if (This.GetType() != value.GetType())
- {
- throw new ArgumentException("The checked flag is not from the same type as the checked variable.");
- }
-
- return (Convert.ToUInt64(This) & Convert.ToUInt64(value)) != 0;
- }
-#endif
}
}
View
21 ReactiveUI/ContractStubs.cs
@@ -1,23 +1,6 @@
using System;
-#if DOTNETISOLDANDSAD || WP7
-
-namespace System.Diagnostics.Contracts
-{
- internal class ContractInvariantMethodAttribute : Attribute {}
-
- internal class Contract
- {
- public static void Requires(bool b, string s = null) {}
- public static void Ensures(bool b, string s = null) {}
- public static void Invariant(bool b, string s = null) {}
- public static T Result<T>() { return default(T); }
- }
-}
-
-#endif
-
-#if IOS || WINRT || PORTABLE
+#if IOS || PORTABLE
namespace ReactiveUI
{
@@ -43,7 +26,7 @@ public interface INotifyPropertyChanging
}
#endif
-#if SILVERLIGHT || WINRT || PORTABLE
+#if PORTABLE
namespace ReactiveUI
{
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
View
14 ReactiveUI/ObservableAsPropertyHelper.cs
@@ -171,20 +171,10 @@ public static class OAPHCreationHelperMixin
TObj source,
Expression<Func<TObj, TRet>> property,
TRet initialValue = default(TRet),
- IScheduler scheduler = null,
- bool setViaReflection = true)
+ IScheduler scheduler = null)
where TObj : ReactiveObject
{
- var ret = source.ObservableToProperty(This, property, initialValue, scheduler);
-
- string propName = Reflection.SimpleExpressionToPropertyName(property);
-
- if (setViaReflection) {
- var fi = Reflection.GetBackingFieldInfoForProperty<TObj>(propName, true);
- if (fi != null) fi.SetValue(source, ret);
- }
-
- return ret;
+ return source.ObservableToProperty(This, property, initialValue, scheduler);
}
}
}
View
160 ReactiveUI/ReactiveObject.cs
@@ -197,161 +197,17 @@ public static class ReactiveObjectExpressionMixin
{
/// <summary>
/// RaiseAndSetIfChanged fully implements a Setter for a read-write
- /// property on a ReactiveObject, making the assumption that the
- /// property has a backing field named "_NameOfProperty". To change this
- /// assumption, set RxApp.GetFieldNameForPropertyNameFunc.
+ /// property on a ReactiveObject, using CallerMemberName to raise the notification
+ /// and the ref to the backing field to set the property.
/// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
- /// <param name="newValue">The new value to set the property to, almost
- /// always the 'value' keyword.</param>
- /// <returns>The newly set value, normally discarded.</returns>
- public static TRet RaiseAndSetIfChanged<TObj, TRet>(
- this TObj This,
- Expression<Func<TObj, TRet>> property,
- TRet newValue)
- where TObj : ReactiveObject
- {
- Contract.Requires(This != null);
- Contract.Requires(property != null);
-
- FieldInfo field;
- string prop_name = Reflection.SimpleExpressionToPropertyName(property);
-
- field = Reflection.GetBackingFieldInfoForProperty<TObj>(prop_name);
-
- var field_val = field.GetValue(This);
-
- if (EqualityComparer<TRet>.Default.Equals((TRet)field_val, (TRet)newValue)) {
- return newValue;
- }
-
- This.raisePropertyChanging(prop_name);
- field.SetValue(This, newValue);
- This.raisePropertyChanged(prop_name);
-
- return newValue;
- }
-
-
- /// <summary>
- /// RaiseAndSetIfChanged fully implements a Setter for a read-write
- /// property on a ReactiveObject, making the assumption that the
- /// property has a backing field named "_NameOfProperty". To change this
- /// assumption, set RxApp.GetFieldNameForPropertyNameFunc. This
- /// overload is intended for Silverlight and WP7 where reflection
- /// cannot access the private backing field.
- /// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
- /// <param name="backingField">A Reference to the backing field for this
- /// property.</param>
- /// <param name="newValue">The new value to set the property to, almost
- /// always the 'value' keyword.</param>
- /// <returns>The newly set value, normally discarded.</returns>
- public static TRet RaiseAndSetIfChanged<TObj, TRet>(
- this TObj This,
- Expression<Func<TObj, TRet>> property,
- ref TRet backingField,
- TRet newValue)
- where TObj : ReactiveObject
- {
- Contract.Requires(This != null);
- Contract.Requires(property != null);
-
- if (EqualityComparer<TRet>.Default.Equals(backingField, newValue)) {
- return newValue;
- }
-
- string prop_name = Reflection.SimpleExpressionToPropertyName(property);
-
- This.raisePropertyChanging(prop_name);
- backingField = newValue;
- This.raisePropertyChanged(prop_name);
- return newValue;
- }
-
- /// <summary>
- /// Use this method in your ReactiveObject classes when creating custom
- /// properties where raiseAndSetIfChanged doesn't suffice.
- /// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
- public static void RaisePropertyChanging<TObj, TRet>(
- this TObj This,
- Expression<Func<TObj, TRet>> property)
- where TObj : ReactiveObject
- {
- var propName = Reflection.SimpleExpressionToPropertyName(property);
- This.raisePropertyChanging(propName);
- }
-
- /// <summary>
- /// Use this method in your ReactiveObject classes when creating custom
- /// properties where raiseAndSetIfChanged doesn't suffice.
- /// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
- public static void RaisePropertyChanged<TObj, TRet>(
- this TObj This,
- Expression<Func<TObj, TRet>> property)
- where TObj : ReactiveObject
- {
- var propName = Reflection.SimpleExpressionToPropertyName(property);
- This.raisePropertyChanged(propName);
- }
-
- /// <summary>
- /// RaiseAndSetIfChanged fully implements a Setter for a read-write
- /// property on a ReactiveObject, making the assumption that the
- /// property has a backing field named "_NameOfProperty". To change this
- /// assumption, set RxApp.GetFieldNameForPropertyNameFunc.
- /// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
- /// <param name="newValue">The new value to set the property to, almost
- /// always the 'value' keyword.</param>
- /// <returns>The newly set value, normally discarded.</returns>
- public static TRet RaiseAndSetIfChanged<TObj, TRet>(
- this TObj This,
- TRet newValue,
- [CallerMemberName] string propertyName = null
- )
- where TObj : ReactiveObject
- {
- Contract.Requires(This != null);
- Contract.Requires(propertyName != null);
-
- var fi = Reflection.GetBackingFieldInfoForProperty<TObj>(propertyName);
-
- var field_val = fi.GetValue(This);
-
- if (EqualityComparer<TRet>.Default.Equals((TRet)field_val, (TRet)newValue)) {
- return newValue;
- }
-
- This.raisePropertyChanging(propertyName);
- fi.SetValue(This, newValue);
- This.raisePropertyChanged(propertyName);
-
- return newValue;
- }
-
-
- /// <summary>
- /// RaiseAndSetIfChanged fully implements a Setter for a read-write
- /// property on a ReactiveObject, making the assumption that the
- /// property has a backing field named "_NameOfProperty". To change this
- /// assumption, set RxApp.GetFieldNameForPropertyNameFunc. This
- /// overload is intended for Silverlight and WP7 where reflection
- /// cannot access the private backing field.
- /// </summary>
- /// <param name="property">An Expression representing the property (i.e.
- /// 'x => x.SomeProperty'</param>
+ /// <typeparam name="TObj">The type of the This.</typeparam>
+ /// <typeparam name="TRet">The type of the return value.</typeparam>
+ /// <param name="This">The <see cref="ReactiveObject"/> raising the notification.</param>
/// <param name="backingField">A Reference to the backing field for this
/// property.</param>
- /// <param name="newValue">The new value to set the property to, almost
- /// always the 'value' keyword.</param>
+ /// <param name="newValue">The new value.</param>
+ /// <param name="propertyName">The name of the property, usually
+ /// automatically provided through the CallerMemberName attribute.</param>
/// <returns>The newly set value, normally discarded.</returns>
public static TRet RaiseAndSetIfChanged<TObj, TRet>(
this TObj This,
View
64 ReactiveUI/Reflection.cs
@@ -11,12 +11,6 @@ namespace ReactiveUI
{
public static class Reflection
{
- #if SILVERLIGHT || WINRT
- static MemoizingMRUCache<Tuple<Type, string>, FieldInfo> backingFieldInfoTypeCache =
- new MemoizingMRUCache<Tuple<Type,string>, FieldInfo>(
- (x, _) => (x.Item1).GetField(RxApp.GetFieldNameForProperty(x.Item2)),
- 15 /*items*/);
-
static readonly MemoizingMRUCache<Tuple<Type, string>, Func<object, object>> propReaderCache =
new MemoizingMRUCache<Tuple<Type, string>, Func<object, object>>((x,_) => {
var fi = (x.Item1).GetField(x.Item2, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
@@ -46,44 +40,6 @@ public static class Reflection
return null;
}, 15);
- #else
- static readonly MemoizingMRUCache<Tuple<Type, string>, FieldInfo> backingFieldInfoTypeCache =
- new MemoizingMRUCache<Tuple<Type, string>, FieldInfo>((x, _) => {
- var fieldName = RxApp.GetFieldNameForProperty(x.Item2);
- var ret = (x.Item1).GetField(fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- return ret;
- }, 50/*items*/);
-
- static readonly MemoizingMRUCache<Tuple<Type, string>, Func<object, object>> propReaderCache =
- new MemoizingMRUCache<Tuple<Type, string>, Func<object, object>>((x,_) => {
- var fi = (x.Item1).GetField(x.Item2, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
- if (fi != null) {
- return (fi.GetValue);
- }
-
- var pi = GetSafeProperty(x.Item1, x.Item2, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
- if (pi != null) {
- return (y => pi.GetValue(y, null));
- }
-
- return null;
- }, 50);
-
- static readonly MemoizingMRUCache<Tuple<Type, string>, Action<object, object>> propWriterCache =
- new MemoizingMRUCache<Tuple<Type, string>, Action<object, object>>((x,_) => {
- var fi = (x.Item1).GetField(x.Item2, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
- if (fi != null) {
- return (fi.SetValue);
- }
-
- var pi = GetSafeProperty(x.Item1, x.Item2, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
- if (pi != null) {
- return ((y,v) => pi.SetValue(y, v, null));
- }
-
- return null;
- }, 50);
- #endif
public static string SimpleExpressionToPropertyName<TObj, TRet>(Expression<Func<TObj, TRet>> property)
{
@@ -176,25 +132,7 @@ public static Type[] GetTypesForPropChain(Type startingType, string[] propNames)
throw new ArgumentException("Property expression must be of the form 'x => x.SomeProperty.SomeOtherProperty'");
}).Skip(1).ToArray();
- }
-
- public static FieldInfo GetBackingFieldInfoForProperty<TObj>(string propName, bool dontThrow = false)
- where TObj : IReactiveNotifyPropertyChanged
- {
- Contract.Requires(propName != null);
- FieldInfo field;
-
- lock(backingFieldInfoTypeCache) {
- field = backingFieldInfoTypeCache.Get(new Tuple<Type, string>(typeof(TObj), propName));
- }
-
- if (field == null && !dontThrow) {
- throw new ArgumentException("You must declare a backing field for this property named: " +
- RxApp.GetFieldNameForProperty(propName));
- }
-
- return field;
- }
+ }
public static Func<TObj, object> GetValueFetcherForProperty<TObj>(string propName)
{
View
24 ReactiveUI/RxApp.cs
@@ -50,10 +50,6 @@ public static class RxApp
{
static RxApp()
{
- // Default name for the field backing the "Foo" property => "_Foo"
- // This is used for ReactiveObject's RaiseAndSetIfChanged mixin
- GetFieldNameForPropertyNameFunc = new Func<string,string>(x => "_" + x);
-
#if WP7
TaskpoolScheduler = new EventLoopScheduler();
#elif WP8
@@ -214,12 +210,6 @@ static RxApp()
}
/// <summary>
- /// Set this property to override the default field naming convention
- /// of "_PropertyName" with a custom one.
- /// </summary>
- public static Func<string, string> GetFieldNameForPropertyNameFunc { get; set; }
-
- /// <summary>
/// This method allows you to override the return value of
/// RxApp.InUnitTestRunner - a null value means that InUnitTestRunner
/// will determine this using its normal logic.
@@ -257,20 +247,6 @@ public static bool InUnitTestRunner()
}
- /// <summary>
- /// GetFieldNameForProperty returns the corresponding backing field name
- /// for a given property name, using the convention specified in
- /// GetFieldNameForPropertyNameFunc.
- /// </summary>
- /// <param name="propertyName">The name of the property whose backing
- /// field needs to be found.</param>
- /// <returns>The backing field name.</returns>
- public static string GetFieldNameForProperty(string propertyName)
- {
- return GetFieldNameForPropertyNameFunc(propertyName);
- }
-
-
//
// Service Location
//

0 comments on commit 0a67c61

Please sign in to comment.
Something went wrong with that request. Please try again.