Esempio n. 1
0
        public void PropertyObjectTypeConversion()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propRO   = instType.GetProperty("MyReadOnlyProperty");
            PropertyInfo propRW   = instType.GetProperty("MyProperty");

            Assert.IsNotNull(propRO);

            inst.MyWriteOnlyProperty = 999;

            PropertyGetter <PropertyTests, object> getter = propRO.CreatePropertyGetter <PropertyTests, object>();

            Assert.IsNotNull(getter);

            object actual = getter(inst);

            Assert.IsNotNull(actual);

            Assert.AreEqual("999", actual);

            PropertySetter <PropertyTests, object> setter = propRW.CreatePropertySetter <PropertyTests, object>();

            Assert.IsNotNull(setter);

            setter(inst, 123);

            Assert.AreEqual(123, inst.MyProperty);
        }
Esempio n. 2
0
        public void ReadOnlyProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propRO   = instType.GetProperty("MyReadOnlyProperty");

            Assert.IsNotNull(propRO);

            inst.MyWriteOnlyProperty = 999;

            PropertyGetter <PropertyTests, string> getter = propRO.CreatePropertyGetter <PropertyTests, string>();

            Assert.IsNotNull(getter);

            string actual = getter(inst);

            Assert.AreEqual("999", actual);

            try
            {
                propRO.CreatePropertySetter <PropertyTests, string>();
                Assert.Fail("Setter should not exist.");
            }
            catch (ArgumentException)
            {
            }

            propRO = instType.GetProperty("MyReadOnlyAutoProperty");

            Assert.IsNotNull(propRO);

            getter = propRO.CreatePropertyGetter <PropertyTests, string>();

            Assert.IsNotNull(getter);

            Assert.AreEqual("MyAuto", getter(inst));

            PropertySetter <PropertyTests, string> setter = propRO.CreatePropertySetter <PropertyTests, string>();

            Assert.IsNotNull(setter);

            setter(inst, "NoAuto");

            Assert.AreEqual("NoAuto", inst.MyReadOnlyAutoProperty);
        }
Esempio n. 3
0
        public void ReadWriteProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propRW   = instType.GetProperty("MyProperty");

            Assert.IsNotNull(propRW);

            PropertyGetter <PropertyTests, int> getter = propRW.CreatePropertyGetter <PropertyTests, int>();
            PropertySetter <PropertyTests, int> setter = propRW.CreatePropertySetter <PropertyTests, int>();

            Assert.IsNotNull(getter);
            Assert.IsNotNull(setter);

            setter(inst, 456);

            Assert.AreEqual(456, inst.MyProperty);

            int actual = Convert.ToInt32(getter(inst));

            Assert.AreEqual(456, actual);
        }
Esempio n. 4
0
        public void WriteOnlyProperty()
        {
            var          inst     = new PropertyTests();
            Type         instType = inst.GetType();
            PropertyInfo propWO   = instType.GetProperty("MyWriteOnlyProperty");

            Assert.IsNotNull(propWO);

            PropertySetter <PropertyTests, int> setter = propWO.CreatePropertySetter <PropertyTests, int>();

            Assert.IsNotNull(setter);

            setter(inst, 789);

            Assert.AreEqual("789", inst.MyReadOnlyProperty);

            PropertyGetter <PropertyTests, int> getter = propWO.CreatePropertyGetter <PropertyTests, int>();

            Assert.IsNotNull(getter);

            int actual = Convert.ToInt32(getter(inst));

            Assert.AreEqual(789, actual);
        }