Exemplo n.º 1
0
        public void EmitMethodEmitsStaticStatic()
        {
            // static types cannot be used as type arguments
            //var method = ReflectionUtilities.EmitMethod<StaticClass1, Action>("Method");
            var method = ReflectionUtilities.EmitMethod <Action>(typeof(StaticClass1), "Method");

            method();
        }
Exemplo n.º 2
0
        public void EmitMethodEmitsPrivateMethod()
        {
            var class1 = new Class1();

            var method1 = ReflectionUtilities.EmitMethod <Action <Class1> >("MethodP1");

            method1(class1);

            var method2 = ReflectionUtilities.EmitMethod <Class1, Action>("SMethodP1");

            method2();
        }
Exemplo n.º 3
0
        public void EmitMethodEmitsFromInfo()
        {
            var class1 = new Class1();

            var methodInfo = typeof(Class1).GetMethod("Method1", BindingFlags.Instance | BindingFlags.Public);
            var method1    = ReflectionUtilities.EmitMethod <Action <Class1> >(methodInfo);

            method1(class1);

            methodInfo = typeof(Class1).GetMethod("Method2", BindingFlags.Instance | BindingFlags.Public, null, new [] { typeof(int) }, null);
            var method2 = ReflectionUtilities.EmitMethod <Action <Class1, int> >(methodInfo);

            method2(class1, 42);

            methodInfo = typeof(Class1).GetMethod("Method3", BindingFlags.Instance | BindingFlags.Public);
            var method3 = ReflectionUtilities.EmitMethod <Func <Class1, int> >(methodInfo);

            Assert.AreEqual(42, method3(class1));

            methodInfo = typeof(Class1).GetMethod("Method4", BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(string) }, null);
            var method4 = ReflectionUtilities.EmitMethod <Func <Class1, string, int> >(methodInfo);

            Assert.AreEqual(42, method4(class1, "42"));

            methodInfo = typeof(Class1).GetMethod("SMethod1", BindingFlags.Static | BindingFlags.Public);
            var smethod1 = ReflectionUtilities.EmitMethod <Action>(methodInfo);

            smethod1();

            methodInfo = typeof(Class1).GetMethod("SMethod2", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(int) }, null);
            var smethod2 = ReflectionUtilities.EmitMethod <Action <int> >(methodInfo);

            smethod2(42);

            methodInfo = typeof(Class1).GetMethod("SMethod3", BindingFlags.Static | BindingFlags.Public);
            var smethod3 = ReflectionUtilities.EmitMethod <Func <int> >(methodInfo);

            Assert.AreEqual(42, smethod3());

            methodInfo = typeof(Class1).GetMethod("SMethod4", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string) }, null);
            var smethod4 = ReflectionUtilities.EmitMethod <Func <string, int> >(methodInfo);

            Assert.AreEqual(42, smethod4("42"));

            methodInfo = typeof(StaticClass1).GetMethod("Method", BindingFlags.Static | BindingFlags.Public);
            var method = ReflectionUtilities.EmitMethod <Action>(methodInfo);

            method();
        }
Exemplo n.º 4
0
        public void EmitMethodEmitsStatic()
        {
            var method1 = ReflectionUtilities.EmitMethod <Class1, Action>("SMethod1");

            method1();

            var method2 = ReflectionUtilities.EmitMethod <Class1, Action <int> >("SMethod2");

            method2(42);

            var method3 = ReflectionUtilities.EmitMethod <Class1, Func <int> >("SMethod3");

            Assert.AreEqual(42, method3());

            var method4 = ReflectionUtilities.EmitMethod <Class1, Func <string, int> >("SMethod4");

            Assert.AreEqual(42, method4("42"));
        }
Exemplo n.º 5
0
        public void EmitMethodEmitsInstance()
        {
            var class1 = new Class1();

            var method1 = ReflectionUtilities.EmitMethod <Action <Class1> >("Method1");

            method1(class1);

            var method2 = ReflectionUtilities.EmitMethod <Action <Class1, int> >("Method2");

            method2(class1, 42);

            var method3 = ReflectionUtilities.EmitMethod <Func <Class1, int> >("Method3");

            Assert.AreEqual(42, method3(class1));

            var method4 = ReflectionUtilities.EmitMethod <Func <Class1, string, int> >("Method4");

            Assert.AreEqual(42, method4(class1, "42"));
        }
Exemplo n.º 6
0
 public void EmitMethodReturnsNull()
 {
     Assert.IsNull(ReflectionUtilities.EmitMethod <Action <Class1> >("ZZZ", false));
     Assert.IsNull(ReflectionUtilities.EmitMethod <Action <Class1, int, int> >("Method1", false));
 }
Exemplo n.º 7
0
        public void EmitMethodThrowsIfInvalid()
        {
            var methodInfo = typeof(Class1).GetMethod("Method1", BindingFlags.Instance | BindingFlags.Public);

            Assert.Throws <ArgumentException>(() => ReflectionUtilities.EmitMethod <Action <Class1, int, int> >(methodInfo));
        }
Exemplo n.º 8
0
 public void EmitMethodThrowsIfNotFound()
 {
     Assert.Throws <InvalidOperationException>(() => ReflectionUtilities.EmitMethod <Action <Class1> >("ZZZ"));
     Assert.Throws <InvalidOperationException>(() => ReflectionUtilities.EmitMethod <Action <Class1, int, int> >("Method1"));
 }
    public void EmitMethodEmitsStaticStatic()
    {
        var method = ReflectionUtilities.EmitMethod <Action>(typeof(StaticClass1), "Method");

        method();
    }