Esempio n. 1
0
        public void CallMethod()
        {
            CallMethodTestClass testClass = new CallMethodTestClass();

            Assert.AreEqual(testClass.PublicMethodWithNoParameters(), ExpressionHelper.CallMethod(testClass, "PublicMethodWithNoParameters"));
            Assert.AreEqual(testClass.PublicMethodWithParameters(5), ExpressionHelper.CallMethod(testClass, "PublicMethodWithParameters", 5));
            Assert.AreEqual(testClass.PublicMethodWithParameters(5, 20L), ExpressionHelper.CallMethod(testClass, "PublicMethodWithParameters", 5, 20L));
            Assert.DoesNotThrow(() => ExpressionHelper.CallMethod(testClass, "PublicVoidMethodWithNoParameters"));
        }
Esempio n. 2
0
        private static void RunMethodBenchmark()
        {
            MethodInfo    methodInfo    = typeof(Person).GetMethod("GetName", BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
            MethodInvoker methodInvoker = DynamicMethodHelper.EmitMethodInvoker(typeof(Person), methodInfo);

            object[] emptyParameters = new object[0];

            Dictionary <string, Action> actions = new Dictionary <string, Action>
            {
                { "Direct method", () => new Person().GetName() },
                { "Reflection method", () => methodInfo.Invoke(new Person(), emptyParameters) },
                { "dynamic method", () =>
                  {
                      dynamic person = new Person();
                      string  name   = person.GetName();
                  } },
                { "Labo expression method", () => ExpressionHelper.CallMethod(new Person(), methodInfo) },
                { "Labo method", () => ReflectionHelper.CallMethod(new Person(), methodInfo) },
                { "Labo cached method", () => methodInvoker(new Person()) },
            };

            Execute("Benchmark for Method Invocation", actions);
        }