Exemplo n.º 1
0
    private static int TestReflectionInvoke()
    {
        Console.WriteLine("Testing reflection invoke");

        // Dummy code to make sure the reflection targets are compiled.
        if (String.Empty.Length > 0)
        {
            new InvokeTests().ToString();
            InvokeTests.GetHello(null);
            InvokeTests.GetHelloGeneric <int>(0);
            InvokeTests.GetHelloGeneric <double>(0);
            string unused;
            InvokeTests.GetHelloByRef(null, out unused);
            unused.ToString();
        }

        {
            MethodInfo helloMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHello");
            string     result      = (string)helloMethod.Invoke(null, new object[] { "world" });
            if (result != "Hello world")
            {
                return(Fail);
            }
        }

        {
            MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(int));
            string     result             = (string)helloGenericMethod.Invoke(null, new object[] { 12345 });
            if (result != "Hello 12345")
            {
                return(Fail);
            }
        }

        {
            MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(double));
            string     result             = (string)helloGenericMethod.Invoke(null, new object[] { 3.14 });
            if (result != "Hello 3.14")
            {
                return(Fail);
            }
        }

        {
            MethodInfo helloByRefMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloByRef");
            object[]   args             = new object[] { "world", null };
            helloByRefMethod.Invoke(null, args);
            if ((string)args[1] != "Hello world")
            {
                return(Fail);
            }
        }

        return(Pass);
    }
Exemplo n.º 2
0
        public static void Run()
        {
            Console.WriteLine(nameof(TestReflectionInvoke));

            // Ensure things we reflect on are in the static callgraph
            if (string.Empty.Length > 0)
            {
                new InvokeTests().ToString();
                InvokeTests.GetHello(null);
                InvokeTests.GetHelloGeneric <int>(0);
                InvokeTests.GetHelloGeneric <double>(0);
                string unused;
                InvokeTests.GetHelloByRef(null, out unused);
                unused.ToString();
            }

            {
                MethodInfo helloMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHello");
                string     result      = (string)helloMethod.Invoke(null, new object[] { "world" });
                if (result != "Hello world")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(int));
                string     result             = (string)helloGenericMethod.Invoke(null, new object[] { 12345 });
                if (result != "Hello 12345")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloByRefMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloByRef");
                object[]   args             = new object[] { "world", null };
                helloByRefMethod.Invoke(null, args);
                if ((string)args[1] != "Hello world")
                {
                    throw new Exception();
                }
            }
        }
Exemplo n.º 3
0
        public static unsafe void Run()
        {
            Console.WriteLine(nameof(TestReflectionInvoke));

            // Ensure things we reflect on are in the static callgraph
            if (string.Empty.Length > 0)
            {
                new InvokeTests().ToString();
                InvokeTests.GetHello(null);
                InvokeTests.GetHelloGeneric <int>(0);
                InvokeTests.GetHelloGeneric <string>(null);
                InvokeTests.GetHelloPointer(null);
                InvokeTests.GetHelloPointerToo(null);
                InvokeTests.GetPointer(null, null);
                string unused;
                InvokeTests.GetHelloByRef(null, out unused);
                unused.ToString();
                new InvokeTestsGeneric <object>().GetHello(null);
                new InvokeTestsGeneric <object>().GetHelloGeneric <object>(null);
                new InvokeTestsGeneric <int>().GetHello(null);
                new InvokeTestsGeneric <int>().GetHelloGeneric <double>(0);
            }

            {
                MethodInfo helloMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHello");
                string     result      = (string)helloMethod.Invoke(null, new object[] { "world" });
                if (result != "Hello world")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(int));
                string     result             = (string)helloGenericMethod.Invoke(null, new object[] { 12345 });
                if (result != "Hello 12345")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(string));
                string     result             = (string)helloGenericMethod.Invoke(null, new object[] { "buddy" });
                if (result != "Hello buddy")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(Type));
                string     result             = (string)helloGenericMethod.Invoke(null, new object[] { typeof(string) });
                if (result != "Hello System.String")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloByRefMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloByRef");
                object[]   args             = new object[] { "world", null };
                helloByRefMethod.Invoke(null, args);
                if ((string)args[1] != "Hello world")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloPointerMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloPointer");
                string     resultNull         = (string)helloPointerMethod.Invoke(null, new object[] { null });
                if (resultNull != "Hello 0")
                {
                    throw new Exception();
                }

                string resultVal = (string)helloPointerMethod.Invoke(null, new object[] { Pointer.Box((void *)42, typeof(char *)) });
                if (resultVal != "Hello 42")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloPointerTooMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetHelloPointerToo");
                string     result = (string)helloPointerTooMethod.Invoke(null, new object[] { Pointer.Box((void *)85, typeof(char **)) });
                if (result != "Hello 85")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo getPointerMethod = typeof(InvokeTests).GetTypeInfo().GetDeclaredMethod("GetPointer");
                object     result           = getPointerMethod.Invoke(null, new object[] { Pointer.Box((void *)2018, typeof(void *)), null });
                if (Pointer.Unbox(result) != (void *)2018)
                {
                    throw new Exception();
                }
            }

#if !CODEGEN_CPP
            {
                MethodInfo helloMethod = typeof(InvokeTestsGeneric <string>).GetTypeInfo().GetDeclaredMethod("GetHello");
                string     result      = (string)helloMethod.Invoke(new InvokeTestsGeneric <string>(), new object[] { "world" });
                if (result != "Hello world System.String")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTestsGeneric <string>).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(object));
                string     result             = (string)helloGenericMethod.Invoke(new InvokeTestsGeneric <string>(), new object[] { "world" });
                if (result != "Hello world System.Object")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloMethod = typeof(InvokeTestsGeneric <int>).GetTypeInfo().GetDeclaredMethod("GetHello");
                string     result      = (string)helloMethod.Invoke(new InvokeTestsGeneric <int>(), new object[] { "world" });
                if (result != "Hello world System.Int32")
                {
                    throw new Exception();
                }
            }

            {
                MethodInfo helloGenericMethod = typeof(InvokeTestsGeneric <int>).GetTypeInfo().GetDeclaredMethod("GetHelloGeneric").MakeGenericMethod(typeof(double));
                string     result             = (string)helloGenericMethod.Invoke(new InvokeTestsGeneric <int>(), new object[] { 1.0 });
                if (result != "Hello 1 System.Double")
                {
                    throw new Exception();
                }
            }
#endif
        }