public void GetPropertySetter_NullInput_ThrowsArgumentNullException() { ArgumentNullException ex = Assert.Throws <ArgumentNullException>( delegate() { SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(null); }); Assert.Equal("propertyInfo", ex.ParamName); }
public void GetPropertySetter_1MillionPropertySets_PerformsInAround10ms() { Example instance = new Example(); PropertyInfo propertyInfo = typeof(Example).GetProperty("A", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); for (long i = 0; i < MaxCount; i++) { setter(instance, "alpha"); } }
public void GetPropertySetter_GetterOnlyProperty_ReturnsPropertyValue() { var input = new Example(); var propertyInfo = input.GetType().GetProperty("Three_Getter"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.Null(setter); }
/// <summary> /// Creates a field setter delegate for the specified property or field /// </summary> /// <param name="memberInfo">PropertyInfo or FieldInfo</param> /// <returns>SetterDelegate for property or field, null otherwise</returns> public static SetterDelegate GetSetter(MemberInfo memberInfo) { if (memberInfo is PropertyInfo) { return(DynamicMethodGenerator.GetPropertySetter((PropertyInfo)memberInfo)); } if (memberInfo is FieldInfo) { return(DynamicMethodGenerator.GetFieldSetter((FieldInfo)memberInfo)); } return(null); }
public void GetPropertySetter_PrivateSetterProperty_ReturnsPropertyValue() { var input = new Example(); var expected = "sun"; var propertyInfo = input.GetType().GetProperty("Re"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); Assert.Equal(expected, input.Re); }
public void GetPropertySetter_AutoImplementedProperty_ReturnsPropertyValue() { var input = new Example(); var expected = "deer"; var propertyInfo = input.GetType().GetProperty("Do"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); Assert.Equal(expected, input.Do); }
public void GetPropertySetter_SetterOnlyProperty_ReturnsPropertyValue() { var input = new Example(); var expected = -3; var propertyInfo = input.GetType().GetProperty("Three_Setter"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); Assert.Equal(expected, input.Three); }
public void GetPropertySetter_PublicReferenceTypeProperty_ReturnsPropertyValue() { var input = new Example(); var expected = "alpha"; var propertyInfo = input.GetType().GetProperty("A"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); Assert.Equal(expected, input.A); }
public void GetPropertySetter_PrivateGetterProperty_ReturnsPropertyValue() { var input = new Example(); var expected = "myself"; var propertyInfo = input.GetType().GetProperty("Mi"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); MethodInfo getter = propertyInfo.GetGetMethod(true); Assert.NotNull(getter); Assert.Equal(expected, getter.Invoke(input, null)); }
public void GetPropertySetter_PrivateProperty_ReturnsPropertyValue() { var input = new Example(); var expected = -2; var propertyInfo = input.GetType().GetProperty("Two", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); setter(input, expected); MethodInfo getter = propertyInfo.GetGetMethod(true); Assert.NotNull(getter); Assert.Equal(expected, getter.Invoke(input, null)); }
public void GetPropertySetter_StaticProperty_ChangesFieldValue() { var propertyInfo = typeof(Example).GetProperty("Solo"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.NotNull(setter); var prev = Example.Solo; try { setter(null, "Duet"); Assert.Equal("Duet", Example.Solo); } finally { Example.Solo = prev; } }
public void GetPropertySetter_AnonymousObjectProperty_ReturnsPropertyValue() { var input = new { A = "aye", B = "bee", C = "sea", One = 1, Two = 2, Three = 3, Do = "doe", Re = "ray", Mi = "me" }; var propertyInfo = input.GetType().GetProperty("Two"); Assert.NotNull(propertyInfo); SetterDelegate setter = DynamicMethodGenerator.GetPropertySetter(propertyInfo); Assert.Null(setter); }