public void CreateGenericPropertySetterForStruct() { var prop = typeof(IInternalInterface).GetProperty("PublicProp"); var setter = PropertyAccessorGenerator.CreateSetter <object, int>(prop, false); object s = new InternalStruct(); //the value type must be boxed setter(s, 123); Assert.AreEqual(123, ((InternalStruct)s).PublicProp); var propWithPrivateSetter = typeof(InternalStruct).GetProperty("PropWithPrivateSetter"); var privateSetter = PropertyAccessorGenerator.CreateSetter <object, int>(propWithPrivateSetter, true); privateSetter(s, 234); Assert.AreEqual(234, ((InternalStruct)s).PropWithPrivateSetter); var stringProp = typeof(InternalStruct).GetProperty("StringProp"); var stringSetter = PropertyAccessorGenerator.CreateSetter <object, string>(stringProp, false); stringSetter(s, "some string"); Assert.AreEqual("some string", ((InternalStruct)s).StringProp); var objectValueSetter = PropertyAccessorGenerator.CreateSetter <object, object>(prop, false); objectValueSetter(s, 321); Assert.AreEqual(321, ((InternalStruct)s).PublicProp); }
public void CreateGenericPropertySetterThrowsException() { var prop = typeof(IInternalInterface).GetProperty("PublicProp"); Assert.Throws <ArgumentException>( () => PropertyAccessorGenerator.CreateSetter <InternalStruct, int>(prop, false)); Assert.Throws <ArgumentException>( () => PropertyAccessorGenerator.CreateSetter <IInternalInterface, Type>(prop, false)); var propWithNoGetter = typeof(IInternalInterface).GetProperty("PropWithNoSetter"); Assert.Throws <ArgumentException>( () => PropertyAccessorGenerator.CreateSetter <IInternalInterface, int>(propWithNoGetter, true)); var propWithPrivateGetter = typeof(InternalClass).GetProperty("PropWithPrivateSetter"); Assert.Throws <ArgumentException>( () => PropertyAccessorGenerator.CreateSetter <InternalClass, int>(propWithPrivateGetter, false)); Assert.Throws <ArgumentException>( () => PropertyAccessorGenerator.CreateSetter <IInternalInterface, int>(propWithPrivateGetter, false)); var setter = PropertyAccessorGenerator.CreateSetter <object, int>(prop, false); Assert.Throws <InvalidCastException>(() => setter(new object(), 1)); Assert.Throws <InvalidCastException>(() => setter(1, 1)); Assert.Throws <NullReferenceException>(() => setter(null, 1)); }
public void CreateSetterForStaticProperties() { var propOfClass = typeof(InternalClass).GetProperty("StaticProp"); var getterForClass = PropertyAccessorGenerator.CreateSetter(propOfClass, false); getterForClass(null, 12345); Assert.AreEqual(12345, InternalClass.StaticProp); var propOfStruct = typeof(InternalStruct).GetProperty("StaticProp"); var getterForStruct = PropertyAccessorGenerator.CreateSetter(propOfStruct, false); getterForStruct(null, 54321); Assert.AreEqual(54321, InternalStruct.StaticProp); }
public void CreateGenericPropertySetterForClass() { var prop = typeof(IInternalInterface).GetProperty("PublicProp"); var setter = PropertyAccessorGenerator.CreateSetter <InternalClass, int>(prop, false); var c = new InternalClass(); setter(c, 123); Assert.AreEqual(123, c.PublicProp); var propWithPrivateSetter = typeof(InternalClass).GetProperty("PropWithPrivateSetter"); var privateSetter = PropertyAccessorGenerator.CreateSetter <InternalClass, int>(propWithPrivateSetter, true); privateSetter(c, 234); Assert.AreEqual(234, c.PropWithPrivateSetter); var stringProp = typeof(InternalClass).GetProperty("StringProp"); var stringSetter = PropertyAccessorGenerator.CreateSetter <InternalClass, string>(stringProp, false); stringSetter(c, "some string"); Assert.AreEqual("some string", c.StringProp); stringSetter(c, null); Assert.AreEqual(null, c.StringProp); }