예제 #1
0
        public static void ILConstructorTest(int iters)
        {
            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);

            object test = ctor();

            if (!(test is ILTest))
            {
                throw new Exception();
            }

            RunTest("Constructor", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object obj = Activator.CreateInstance(type);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object obj = ctor();
                }
            }
                    );
        }
예제 #2
0
 public void Setup()
 {
     TestType     = typeof(ILTest);
     ctor         = ILTools.GetConstructor <ILDelegates.TypeConstructor>(TestType);
     fi           = TestType.GetField("ValueField");
     fv           = ILTools.GetFieldValue <ILDelegates.MethodDel>(TestType, fi);
     instance     = (ILTest)ctor();
     pi           = TestType.GetProperty("PropertyField"); getPropVal = ILTools.GetPropertyGet <ILDelegates.MethodDel>(TestType, pi);
     setPropVal   = ILTools.GetPropertySet <ILDelegates.MethodDel_v <string> >(TestType, pi);
     paramLessMi  = TestType.GetMethod("WriteMessage");
     paramLessDel = ILTools.GetMethodDel <ILDelegates.MethodDel>(TestType, paramLessMi);
     paramMi      = TestType.GetMethod("TestMethod");
     paramDel     = ILTools.GetMethodDel <ILDelegates.MethodDel_v <int> >(TestType, paramMi);
 }
예제 #3
0
        public void ILConstructorTest()
        {
            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);

            object test = ctor();

            if (!(test is ILTest))
            {
                throw new Exception();
            }

            RunBenchmark("Constructor", "Reflection", "IL", () =>
            {
                object obj = Activator.CreateInstance(type);
            }, () =>
            {
                object obj = ctor();
            });
        }
예제 #4
0
        public static void ILTests(int iters)
        {
            ILConstructorTest(iters);


            Type type = typeof(ILTest);

            ILDelegates.TypeConstructor ctor = ILTools.GetConstructor <ILDelegates.TypeConstructor>(type);
            FieldInfo    fi       = type.GetField("ValueField");
            PropertyInfo pi       = type.GetProperty("PropertyField");
            MethodInfo   mi       = type.GetMethod("WriteMessage");
            MethodInfo   testMeth = type.GetMethod("TestMethod");



            ILTest obj = (ILTest)ctor();

            obj.ValueField    = "123";
            obj.PropertyField = "456";

            ILDelegates.MethodDel fv = ILTools.GetFieldValue <ILDelegates.MethodDel>(type, fi);
            object fieldSanityCheck  = fi.GetValue(obj);

            if (fieldSanityCheck is string str)
            {
                if (str != "123")
                {
                    throw new Exception();
                }
            }

            RunTest("Getting Field Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = fi.GetValue(obj);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = fv(obj);
                }
            });


            ILDelegates.MethodDel getPF = ILTools.GetPropertyGet <ILDelegates.MethodDel>(type, pi);

            object getPropertySanityCheck = getPF(obj);

            if (getPropertySanityCheck is string propertyValue)
            {
                if (propertyValue != "456")
                {
                    throw new Exception();
                }
            }

            RunTest("Get Property Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = pi.GetValue(obj);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object ret = getPF(obj);
                }
            });



            ILDelegates.MethodDel_v <string> setPF = ILTools.GetPropertySet <ILDelegates.MethodDel_v <string> >(type, pi);

            setPF(obj, "EEEEEEE");
            if (obj.PropertyField != "EEEEEEE")
            {
                throw new Exception();
            }


            RunTest("Set Property Value", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    pi.SetValue(obj, "EEEEEEE");
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    setPF(obj, "EEEEEEE");
                }
            });

            ILDelegates.MethodDel write = ILTools.GetMethodDel <ILDelegates.MethodDel>(type, mi);
            object funcCallSanityCheck  = write(obj);

            if (funcCallSanityCheck is string funcRet)
            {
                if (funcRet != "")
                {
                    throw new Exception();
                }
            }

            RunTest("Function Call", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object methRet = mi.Invoke(obj, new object[0]);
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    object methRet = write(obj);
                }
            });


            ILDelegates.MethodDel_v <int> func = ILTools.GetMethodDel <ILDelegates.MethodDel_v <int> >(type, testMeth);

            RunTest("Function Call with Parameter", "Reflection", "IL",
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    testMeth.Invoke(obj, new object[] { 100 });
                }
            },
                    () =>
            {
                for (int i = 0; i < iters; i++)
                {
                    func(obj, 100);
                }
            });
        }