public static void TestNonStaticField()
            {
                TestClass obj = new TestClass();

                obj.NonStaticIntField = -1;

                FieldInfo field = typeof(TestClass).GetField("NonStaticIntField");;
                DynamicMethodProxyHandler handler = fac.GetFieldSetDelegate(field);

                handler(obj, new object[] { 5 });
                Check.Assert(obj.NonStaticIntField == 5);
                handler = fac.GetFieldGetDelegate(field);
                Check.Assert(((int)handler(obj, null)) == 5);
            }
            public static void TestNonStaticProperty()
            {
                TestClass obj = new TestClass();

                obj.NonStaticIntField = -1;

                PropertyInfo property             = typeof(TestClass).GetProperty("NonStaticIntProperty");;
                DynamicMethodProxyHandler handler = fac.GetMethodDelegate(property.GetSetMethod());

                handler(obj, new object[] { 5 });
                Check.Assert(obj.NonStaticIntField == 5);
                handler = fac.GetMethodDelegate(property.GetGetMethod());
                Check.Assert(((int)handler(obj, null)) == 5);
            }
예제 #3
0
        public object CreateInstance(Module targetModule, string typeFullName, bool ignoreCase, bool isPublic, Binder binder, System.Globalization.CultureInfo culture, object[] activationAttrs, params object[] paramObjs)
        {
            if (targetModule == null)
            {
                throw new ArgumentNullException("targetModule");
            }

            //get method info of Assembly.CreateInstance() method first
            MethodInfo mi = ReflectionUtils.GetMethodInfoFromArrayBySignature(
                "System.Object CreateInstance(System.String, Boolean, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, System.Object[])",
                typeof(Assembly).GetMethods());

            DynamicMethodProxyHandler dmd = GetMethodDelegate(targetModule, mi);

            return(dmd(targetModule.Assembly, typeFullName, ignoreCase, BindingFlags.Instance | (isPublic ? BindingFlags.Public : BindingFlags.NonPublic), binder, paramObjs, culture, activationAttrs));
        }
            public static void TestNonStaticMethod()
            {
                TestClass obj = new TestClass();

                DynamicMethodProxyHandler handler = fac.GetMethodDelegate(typeof(TestClass).GetMethod("NonStaticReturnVoidMethod"));

                handler(obj, null);

                object[] inputParams = new object[] { "str", 1, null, null };
                handler = fac.GetMethodDelegate(typeof(TestClass).GetMethod("NonStaticReturnIntMethod"));
                object ret = handler(obj, inputParams);

                Check.Assert(((int)inputParams[2]) == 2);
                Check.Assert(((string)inputParams[3]) == "outstr");
                Check.Assert(((int)ret) == 3);
            }