Пример #1
0
        public void TestCanExecuteGenericMethodOnObject()
        {
            ReflectTestClass myTestClass = new ReflectTestClass();

            //test can do method with void return type
            myTestClass.HasMethodExecuted = false;
            Assert.IsFalse(myTestClass.HasMethodExecuted);
            object returnValue = ReflectionUtility.Reflect.InvokeGenericMethodOnObject(myTestClass, "ExecuteMe", typeof(ReflectTestClass), new object[] { new ReflectTestClass("Hello") });

            Assert.IsNull(returnValue);
            Assert.IsTrue(myTestClass.HasMethodExecuted);

            //test can do method with return type T and no params
            myTestClass.HasMethodExecuted = false;
            Assert.IsFalse(myTestClass.HasMethodExecuted);
            returnValue = ReflectionUtility.Reflect.InvokeGenericMethodOnObject(myTestClass, "ExecuteMe", typeof(ReflectTestClass), null);
            Assert.IsNotNull(returnValue);
            Assert.IsInstanceOf <ReflectTestClass>(returnValue);
            Assert.IsTrue(myTestClass.HasMethodExecuted);

            //test can do method with return type T and params
            myTestClass.HasMethodExecuted = false;
            Assert.IsFalse(myTestClass.HasMethodExecuted);
            returnValue = ReflectionUtility.Reflect.InvokeGenericMethodOnObject(myTestClass, "ExecuteMe", typeof(ReflectTestClass), new object[] { new ReflectTestClass("Hello"), new ReflectTestClass("goodBye") });
            Assert.IsNotNull(returnValue);
            Assert.IsInstanceOf <ReflectTestClass>(returnValue);
            Assert.IsTrue(myTestClass.HasMethodExecuted);
        }
Пример #2
0
        public void TestCanGetPrivatePropertyValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass("Bob");

            string propertyValue = ReflectionUtility.Reflect.GetPropertyValue <String>(myTestClass, "NamePropertyPrivate");

            Assert.AreEqual("Bob", propertyValue);
        }
Пример #3
0
        public void TestCanSetPrivatePropertyValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass();

            Assert.IsNullOrEmpty(ReflectionUtility.Reflect.GetPropertyValue <String>(myTestClass, "NamePropertyPrivate"));
            ReflectionUtility.Reflect.AssignPropertyValue(myTestClass, "NamePropertyPrivate", "Bob");
            Assert.AreEqual("Bob", ReflectionUtility.Reflect.GetPropertyValue <String>(myTestClass, "NamePropertyPrivate"));
        }
Пример #4
0
        public void TestCanGetPrivateFieldValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass("Bob");

            string fieldValue = ReflectionUtility.Reflect.GetFieldValue <String>(myTestClass, "NameFieldPrivate");

            Assert.AreEqual("Bob", fieldValue);
        }
Пример #5
0
        public void TestCanSetPublicPropertyValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass();

            Assert.IsNullOrEmpty(myTestClass.NamePropertyPublic);
            ReflectionUtility.Reflect.AssignPropertyValue(myTestClass, "NamePropertyPublic", "Bob");
            Assert.IsNotNullOrEmpty(myTestClass.NamePropertyPublic);
            Assert.AreEqual("Bob", myTestClass.NamePropertyPublic);
        }
Пример #6
0
        public void TestCanGetPrivateFieldInfo()
        {
            ReflectTestClass myTestClass = new ReflectTestClass("Bob");

            FieldInfo fieldInfo = ReflectionUtility.Reflect.GetField(myTestClass, "NameFieldPrivate");

            Assert.IsNotNull(fieldInfo);
            Assert.AreEqual("Bob", fieldInfo.GetValue(myTestClass).ToString());
        }
Пример #7
0
        public void TestCanGetPublicPropertyValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass();

            myTestClass.NamePropertyPublic = "Bob";

            Assert.IsNotNullOrEmpty(myTestClass.NamePropertyPublic);
            string propertyValue = ReflectionUtility.Reflect.GetPropertyValue <String>(myTestClass, "NamePropertyPublic");

            Assert.AreEqual("Bob", propertyValue);
        }
Пример #8
0
        public void TestCanGetTypeOfValueForGivenObject()
        {
            ReflectTestClass myTestClass = new ReflectTestClass();

            Type publicPropertyType = ReflectionUtility.Reflect.GetValueType(myTestClass, "NamePropertyPublic");

            Assert.IsTrue(publicPropertyType == typeof(String));

            Type publicFieldType = ReflectionUtility.Reflect.GetValueType(myTestClass, "NameFieldPublic");

            Assert.IsTrue(publicFieldType == typeof(String));
        }
Пример #9
0
        public void TestCanGetPrivatePropertyInfo()
        {
            ReflectTestClass myTestClass = new ReflectTestClass("Bob");

            PropertyInfo propertyInfo = ReflectionUtility.Reflect.GetProperty(myTestClass, "NamePropertyPrivate");

            Assert.IsNotNull(propertyInfo);
            object value = propertyInfo.GetValue(myTestClass, null);

            Assert.IsNotNull(value);
            Assert.AreEqual("Bob", value.ToString());
        }
Пример #10
0
        public void TestCanGetPropertyOrFieldValue()
        {
            ReflectTestClass myTestClass = new ReflectTestClass("Bob");

            string fieldValue = ReflectionUtility.Reflect.GetValue <String>(myTestClass, "NameFieldPrivate");

            Assert.IsNotNullOrEmpty(fieldValue);
            Assert.AreEqual("Bob", fieldValue);

            string propertyValue = ReflectionUtility.Reflect.GetValue <String>(myTestClass, "NamePropertyPrivate");

            Assert.IsNotNullOrEmpty(propertyValue);
            Assert.AreEqual("Bob", propertyValue);
        }