Example #1
0
        public static MethodInfo GetRuntimeMethod(this Type type, string name, params Type[] parameters)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeMethod(type, name, parameters));
#else
            return(type.GetMethod(name, parameters));
#endif
        }
Example #2
0
        public static MethodInfo GetRuntimeMethod(Type source, string name, params Type[] parameters)
        {
#if NET35 || NET40
            return(source.GetMethod(name, parameters));
#else
            return(RuntimeReflectionExtensions.GetRuntimeMethod(source, name, parameters));
#endif
        }
Example #3
0
            // Interrogate the assembly for any classes to add to the list of file formats. They:
            // 1. Must be nested in a class that
            //    1a. ends with "Format"
            //    1b. derives from FormatBase
            //    1c. contains the method public static CreateModel (Stream, byte[], string)
            // 2. Must be named "Model" and derive from a FormatBase.ModelBase
            // 4. Must contain the property public static string[] Names { get; }
            // 5. May contain the property public static string Subname { get; }
#if NETFX_CORE
            private void LoadFormats()
            {
                foreach (var type in typeof(FormatBase).GetTypeInfo().Assembly.DefinedTypes)
                {
                    if (type.IsClass && type.IsSubclassOf(typeof(FormatBase)) && type.Name.EndsWith("Format"))
                    {
                        MethodInfo createrInfo = null, namesInfo = null, subnameInfo = null;
                        foreach (var meth in type.DeclaredMethods)
                        {
                            if ((meth.Attributes & MethodAttributes.Public | MethodAttributes.Static) == (MethodAttributes.Public | MethodAttributes.Static))
                            {
                                Type ret = meth.ReturnType;
                                if (!meth.IsSpecialName)
                                {
                                    var x2 = ret.Name;
                                    if (meth.Name == "CreateModel")
                                    {
                                        var parm = meth.GetParameters();
                                        var bt   = ret.GetTypeInfo().BaseType;
                                        while (ret.GetTypeInfo().BaseType != typeof(object))
                                        {
                                            ret = ret.GetTypeInfo().BaseType;
                                        }
                                        if (ret == typeof(FormatBase.ModelBase) && parm.Length == 3 &&
                                            parm[0].ParameterType == typeof(Stream) &&
                                            parm[1].ParameterType == typeof(byte[]) &&
                                            parm[2].ParameterType == typeof(string))
                                        {
                                            createrInfo = meth;
                                        }
                                    }
                                }
                                else if (meth.Name == "get_Names" && ret == typeof(string[]))
                                {
                                    namesInfo = meth;
                                }
                                else if (meth.IsSpecialName && meth.Name == "get_Subname" && ret == typeof(string))
                                {
                                    subnameInfo = meth;
                                }
                            }
                        }
                        if (namesInfo != null && createrInfo != null)
                        {
                            var        factorySignature = new Type[] { typeof(Stream), typeof(byte[]), typeof(string) };
                            MethodInfo runInfo          = RuntimeReflectionExtensions.GetRuntimeMethod(type.AsType(), "Create", factorySignature);
                            var        creater          = (FormatModelFactory)createrInfo.CreateDelegate(typeof(FormatModelFactory));
                            var        names            = (string[])namesInfo.Invoke(null, null);
                            var        subname          = (string)(subnameInfo == null ? null : subnameInfo.Invoke(null, null));
                            FormatModel.Add(creater, names, subname);
                        }
                    }
                }

                FormatModel.Sort();
            }
Example #4
0
        public void GetRuntimeMethod()
        {
            var types = GetTypes();

            AssertExtensions.Throws <ArgumentNullException>("type", () =>
            {
                RuntimeReflectionExtensions.GetRuntimeMethod(default(Type), "foo", Type.EmptyTypes);
            });


            AssertExtensions.Throws <ArgumentNullException>("name", () =>
            {
                typeof(RuntimeReflectionExtensionsTests).GetRuntimeMethod(null, Type.EmptyTypes);
            });


            AssertExtensions.Throws <ArgumentNullException>("types", () =>
            {
                typeof(RuntimeReflectionExtensionsTests).GetRuntimeMethod("RunTest_GetRuntimeMethod", null);
            });

            Assert.Null(typeof(TestType).GetRuntimeMethod("", Type.EmptyTypes));

            List <string> methods = new List <string>();

            foreach (TypeInfo type in types)
            {
                if (!type.Namespace.Equals("MethodDefinitions", StringComparison.Ordinal))
                {
                    continue;
                }

                methods.Clear();
                methods.AddRange((IEnumerable <string>)type.GetDeclaredField("PublicMethodNames").GetValue(null));

                foreach (string method in methods)
                {
                    string methodName = GetMethodName(method);
                    Type[] parameters = GetMethodParameters(method);
                    Assert.NotNull(type.AsType().GetRuntimeMethod(methodName, parameters));
                }
            }

            Assert.Equal(typeof(TestType).GetMethod("Flush"), typeof(TestType).GetRuntimeMethod("Flush", Array.Empty <Type>()));
        }
Example #5
0
        public void GetRuntimeMethod()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeMethod(null, "foo", new Type[0]);
            });


            Assert.Throws <ArgumentNullException>(() =>
            {
                typeof(RuntimeReflectionExtensionsTests).GetRuntimeMethod(null, new Type[0]);
            });


            Assert.Throws <ArgumentNullException>(() =>
            {
                typeof(RuntimeReflectionExtensionsTests).GetRuntimeMethod("RunTest_GetRuntimeMethod", null);
            });

            List <String> methods = new List <String>();

            foreach (TypeInfo type in types)
            {
                if (!type.Namespace.Equals("MethodDefinitions", StringComparison.Ordinal))
                {
                    continue;
                }

                methods.Clear();
                methods.AddRange((IEnumerable <String>)type.GetDeclaredField("PublicMethodNames").GetValue(null));

                foreach (String method in methods)
                {
                    String methodName = GetMethodName(method);
                    Type[] parameters = GetMethodParameters(method);
                    Assert.NotNull(type.AsType().GetRuntimeMethod(methodName, parameters));
                }
            }
        }
Example #6
0
 static MethodInfo GetMethod(Type type, string name, Type parameterType)
 {
     return(RuntimeReflectionExtensions.GetRuntimeMethod(type, name, new Type[] { parameterType }));
 }