예제 #1
0
 /// <summary>
 /// Gets the value of the dynamic property for the specified target object.
 /// </summary>
 /// <param name="target">
 /// Target object to get property value from.
 /// </param>
 /// <returns>
 /// A property value.
 /// </returns>
 public object GetValue(object target)
 {
     if (isOptimizedGet)
     {
         return(dynamicProperty.GetValue(target));
     }
     else
     {
         return(propertyInfo.GetValue(target, null));
     }
 }
        public void TestInstanceProperties()
        {
            IDynamicProperty placeOfBirth = Create(typeof(Inventor).GetProperty("PlaceOfBirth"));
            Assert.AreEqual(tesla.PlaceOfBirth, placeOfBirth.GetValue(tesla));

            IDynamicProperty dateOfBirth = Create(typeof(Inventor).GetProperty("DOB"));
            Assert.AreEqual(tesla.DOB, dateOfBirth.GetValue(tesla));
            dateOfBirth.SetValue(tesla, new DateTime(2004, 8, 14));
            Assert.AreEqual(new DateTime(2004, 8, 14), dateOfBirth.GetValue(tesla));

            DateTime mostImportantDayInTheWorldEver = new DateTime(2004, 8, 14);
            IDynamicProperty year = Create(typeof(DateTime).GetProperty("Year"));
            Assert.AreEqual(mostImportantDayInTheWorldEver.Year, year.GetValue(mostImportantDayInTheWorldEver));
        }
 public void AccessInheritedPropertyFromDerivedClass()
 {
     IDynamicProperty p = Create(typeof(BaseClass).GetProperty("MyBaseProperty"));
     ClassWithNonReadableProperty derivedObject = new ClassWithNonReadableProperty();
     derivedObject.MyBaseProperty = "testtext";
     Assert.AreEqual("testtext", p.GetValue(derivedObject));
 }
 public void AccessInheritedPropertyFromBaseClass()
 {
     IDynamicProperty p = Create(typeof(ClassWithNonReadableProperty).GetProperty("MyBaseProperty"));
     BaseClass baseObject = new BaseClass();
     baseObject.MyBaseProperty = "testtext";
     Assert.AreEqual("testtext", p.GetValue(baseObject));
 }
        public void TestNonReadableProperties()
        {
            IDynamicProperty nonReadableProperty =
                Create(typeof(ClassWithNonReadableProperty).GetProperty("MyProperty"));

            nonReadableProperty.GetValue(null);
        }
예제 #6
0
        public void PerformanceTests()
        {
            int    n = 10000000;
            object x = null;

            // tesla.PlaceOfBirth
            start = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                x = tesla.PlaceOfBirth;
            }
            stop = DateTime.Now;
            PrintTest("tesla.PlaceOfBirth (direct)", n, Elapsed);

            start = DateTime.Now;
            IDynamicProperty placeOfBirth = DynamicProperty.Create(typeof(Inventor).GetProperty("PlaceOfBirth"));

            for (int i = 0; i < n; i++)
            {
                x = placeOfBirth.GetValue(tesla);
            }
            stop = DateTime.Now;
            PrintTest("tesla.PlaceOfBirth (dynamic reflection)", n, Elapsed);

            start = DateTime.Now;
            PropertyInfo placeOfBirthPi = typeof(Inventor).GetProperty("PlaceOfBirth");

            for (int i = 0; i < n; i++)
            {
                x = placeOfBirthPi.GetValue(tesla, null);
            }
            stop = DateTime.Now;
            PrintTest("tesla.PlaceOfBirth (standard reflection)", n, Elapsed);
        }
예제 #7
0
        public void AccessOverriddenProperty()
        {
            IDynamicProperty pVirt       = Create(typeof(BaseClass).GetProperty("MyVirtualBaseProperty"));
            IDynamicProperty pOverridden = Create(typeof(ClassWithNonReadableProperty).GetProperty("MyVirtualBaseProperty"));

            Assert.AreEqual("MyVirtualBasePropertyText", pVirt.GetValue(new BaseClass()));
            try
            {
                Assert.AreEqual("MyVirtualBasePropertyText", pOverridden.GetValue(new BaseClass()));
                Assert.Fail();
            }
            catch (InvalidCastException)
            {
            }
            Assert.AreEqual("MyOverridenDerivedPropertyText", pVirt.GetValue(new ClassWithNonReadableProperty()));
            Assert.AreEqual("MyOverridenDerivedPropertyText", pOverridden.GetValue(new ClassWithNonReadableProperty()));
        }
        public void TestForRestrictiveSetter()
        {
            Something something = new Something();

            IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));
            third.SetValue(something, 456);
            //this should be ok, because both get and set of the "Third" property are public
            Assert.AreEqual(456, third.GetValue(something));

            IDynamicProperty second = Create(typeof(Something).GetProperty("Second"));
            Assert.AreEqual(2, second.GetValue(something));
            //this should cause MethodAccessException, because set is private in "Second" property
            second.SetValue(something, 123);

            //this should never execute
            Assert.Throws<MethodAccessException>(() => second.GetValue(something));
        }
        public void TestStaticProperties()
        {
            IDynamicProperty today = Create(typeof(DateTime).GetProperty("Today"));
            Assert.AreEqual(DateTime.Today, today.GetValue(null));

            IDynamicProperty myProperty = Create(typeof(MyStaticClass).GetProperty("MyProperty"));
            myProperty.SetValue(null, "here we go...");
            Assert.AreEqual("here we go...", myProperty.GetValue(null));
        }
예제 #10
0
        public void TestForRestrictiveSetterWithSafeWrapper()
        {
            Something something = new Something();

            IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));

            third.SetValue(something, 456);
            //this should be ok, because both get and set of the "Third" property are public
            Assert.AreEqual(456, third.GetValue(something));

            IDynamicProperty second = Create(typeof(Something).GetProperty("Second"));

            Assert.AreEqual(2, second.GetValue(something));
            //this should not cause MethodAccessException because "second" is created using "CreateSafe"
            second.SetValue(something, 123);

            Assert.AreEqual(123, second.GetValue(something));
        }
예제 #11
0
        public void CanGetSetSimpleProperty()
        {
            if (SystemUtils.MonoRuntime)
            {
                // TODO (EE): find solution for Mono
                return;
            }
            object           o = GetVisualBasicTestObject();
            IDynamicProperty simpleProperty = Create(o.GetType().GetProperty("SimpleProperty"));

            simpleProperty.SetValue(o, "CanGetSimpleText", "args");
            Assert.AreEqual("CanGetSimpleText", ThisLastPropertyValue.GetValue(o));
            Assert.AreEqual("CanGetSimpleText", simpleProperty.GetValue(o));
        }
        public void TestForRestrictiveGetter()
        {
            Something something = new Something();

            IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));
            //this should be ok, because both get and set of the "Third" property are public
            third.SetValue(something, 456);
            Assert.AreEqual(456, third.GetValue(something));

            IDynamicProperty first = Create(typeof(Something).GetProperty("First"));
            first.SetValue(something, 123);
            //this should cause MethodAccessException, because get is private
            Assert.Throws<MethodAccessException>(() => first.GetValue(something));
        }
예제 #13
0
        public void TestForRestrictiveGetterWithSafeWrapper()
        {
            Something something = new Something();
            //new SafeProperty()
            IDynamicProperty third = Create(typeof(Something).GetProperty("Third"));

            //this should be ok, because both get and set of the "Third" property are public
            third.SetValue(something, 456);
            Assert.AreEqual(456, third.GetValue(something));

            IDynamicProperty first = Create(typeof(Something).GetProperty("First"));

            first.SetValue(something, 123);
            //this should not cause MethodAccessException, "first" is createtd using "CreateSafe"
            Assert.AreEqual(123, first.GetValue(something));
        }
예제 #14
0
        public void CanGetSetSimpleIndexer()
        {
            if (SystemUtils.MonoRuntime)
            {
                // TODO (EE): find solution for Mono
                return;
            }

            object           o = GetVisualBasicTestObject();
            IDynamicProperty simpleProperty = Create(o.GetType().GetProperty("SimpleIndexer"));

            // write
            simpleProperty.SetValue(o, "CanGetSetSimpleIndexer", 2);
            Assert.AreEqual("CanGetSetSimpleIndexer", ThisLastPropertyValue.GetValue(o));
            Assert.AreEqual(2, ThisArg1.GetValue(o));

            // read
            object value = simpleProperty.GetValue(o, 3);

            Assert.AreEqual("CanGetSetSimpleIndexer", value);
            Assert.AreEqual(3, ThisArg1.GetValue(o));
        }
예제 #15
0
        public void CanGetSetComplexIndexer()
        {
            if (SystemUtils.MonoRuntime)
            {
                // TODO (EE): find solution for Mono
                return;
            }
            object           o        = GetVisualBasicTestObject();
            IDynamicProperty property = Create(o.GetType().GetProperty("ComplexIndexer"));

            // write
            property.SetValue(o, "CanGetSetComplexIndexer", 2, "Arg2");
            Assert.AreEqual("CanGetSetComplexIndexer", ThisLastPropertyValue.GetValue(o));
            Assert.AreEqual(2.0, (double)ThisArg1.GetValue(o));
            Assert.AreEqual("Arg2", ThisArg2.GetValue(o));

            // read
            object value = property.GetValue(o, 3, "Arg3");

            Assert.AreEqual("CanGetSetComplexIndexer", value);
            Assert.AreEqual(3.0, (double)ThisArg1.GetValue(o));
            Assert.AreEqual("Arg3", ThisArg2.GetValue(o));
        }
예제 #16
0
        public void TestNonReadableProperties()
        {
            IDynamicProperty nonReadableProperty = Create(typeof(ClassWithNonReadableProperty).GetProperty("MyProperty"));

            Assert.Throws <InvalidOperationException>(() => nonReadableProperty.GetValue(null));
        }
예제 #17
0
        /// <summary>
        /// Invokes a property getter using dynamic
        /// method invocation.
        /// </summary>
        /// <param name="obj">Target object.</param>
        /// <param name="property">Property to invoke.</param>
        /// <returns></returns>
        public static object CallPropertyGetter(object objectType, string property)
        {
            IDynamicProperty dynamicProperty = DynamicPropertyCache.GetDynamicProperty(objectType.GetType(), property);

            return(dynamicProperty.GetValue(objectType));
        }