Esempio n. 1
0
        public void CreateDelegate_PublicMethod()
        {
            Type typeTestClass = typeof(MI_BaseClass);

            MI_BaseClass baseClass                 = (MI_BaseClass)Activator.CreateInstance(typeTestClass);
            MethodInfo   virtualMethodInfo         = GetMethod(typeTestClass, nameof(MI_BaseClass.VirtualMethod));
            MethodInfo   privateInstanceMethodInfo = GetMethod(typeTestClass, "PrivateInstanceMethod");
            MethodInfo   publicStaticMethodInfo    = GetMethod(typeTestClass, nameof(MI_BaseClass.PublicStaticMethod));

            Delegate methodDelegate = virtualMethodInfo.CreateDelegate(typeof(Delegate_TC_Int));
            object   returnValue    = ((Delegate_TC_Int)methodDelegate).DynamicInvoke(new object[] { baseClass });

            Assert.Equal(baseClass.VirtualMethod(), returnValue);

            methodDelegate = privateInstanceMethodInfo.CreateDelegate(typeof(Delegate_TC_Int));
            returnValue    = ((Delegate_TC_Int)methodDelegate).DynamicInvoke(new object[] { baseClass });
            Assert.Equal(21, returnValue);

            methodDelegate = virtualMethodInfo.CreateDelegate(typeof(Delegate_Void_Int), baseClass);
            returnValue    = ((Delegate_Void_Int)methodDelegate).DynamicInvoke(null);
            Assert.Equal(baseClass.VirtualMethod(), returnValue);

            methodDelegate = publicStaticMethodInfo.CreateDelegate(typeof(Delegate_Str_Str));
            returnValue    = ((Delegate_Str_Str)methodDelegate).DynamicInvoke(new object[] { "85" });
            Assert.Equal("85", returnValue);

            methodDelegate = publicStaticMethodInfo.CreateDelegate(typeof(Delegate_Void_Str), "93");
            returnValue    = ((Delegate_Void_Str)methodDelegate).DynamicInvoke(null);
            Assert.Equal("93", returnValue);
        }
Esempio n. 2
0
        public void CreateDelegate_ValueTypeParameters()
        {
            MethodInfo   miPublicStructMethod = GetMethod(typeof(MI_BaseClass), nameof(MI_BaseClass.PublicStructMethod));
            MI_BaseClass testClass            = new MI_BaseClass();

            Delegate methodDelegate = miPublicStructMethod.CreateDelegate(typeof(Delegate_DateTime_Str));
            object   returnValue    = ((Delegate_DateTime_Str)methodDelegate).DynamicInvoke(new object[] { testClass, null });

            Assert.Equal(testClass.PublicStructMethod(new DateTime()), returnValue);
        }
Esempio n. 3
0
        public void CreateDelegate_InheritedMethod()
        {
            Type typeTestClass    = typeof(MI_BaseClass);
            Type TestSubClassType = typeof(MI_SubClass);

            MI_SubClass  testSubClass      = (MI_SubClass)Activator.CreateInstance(TestSubClassType);
            MI_BaseClass testClass         = (MI_BaseClass)Activator.CreateInstance(typeTestClass);
            MethodInfo   virtualMethodInfo = GetMethod(typeTestClass, nameof(MI_BaseClass.VirtualMethod));

            Delegate methodDelegate = virtualMethodInfo.CreateDelegate(typeof(Delegate_TC_Int));
            object   returnValue    = ((Delegate_TC_Int)methodDelegate).DynamicInvoke(new object[] { testSubClass });

            Assert.Equal(testSubClass.VirtualMethod(), returnValue);

            methodDelegate = virtualMethodInfo.CreateDelegate(typeof(Delegate_Void_Int), testSubClass);
            returnValue    = ((Delegate_Void_Int)methodDelegate).DynamicInvoke();
            Assert.Equal(testSubClass.VirtualMethod(), returnValue);
        }