示例#1
0
        public void GetRuntimeField()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeField(null, "foo");
            });


            Assert.Throws <ArgumentNullException>(() =>
            {
                typeof(RuntimeReflectionExtensionsTests).GetRuntimeField(null);
            });

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

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

                fields.Clear();
                fields.AddRange((IEnumerable <String>)type.GetDeclaredField("PublicFieldNames").GetValue(null));

                foreach (String fieldName in fields)
                {
                    Assert.NotNull(type.AsType().GetRuntimeField(fieldName));
                }
            }
        }
        public void GetRuntimeFields()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeFields(null);
            });

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

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

                fields.Clear();
                fields.AddRange((IEnumerable <string>)type.GetDeclaredField("DeclaredFieldNames").GetValue(null));
                fields.AddRange((IEnumerable <string>)type.GetDeclaredField("InheritedFieldNames").GetValue(null));
                if (type.GetDeclaredField("NewFieldNames") != null)
                {
                    fields.AddRange((IEnumerable <string>)type.GetDeclaredField("NewFieldNames").GetValue(null));
                }

                Assert.All(type.AsType().GetRuntimeFields(), f => Assert.True(fields.Remove(f.Name)));
                Assert.Empty(fields);
            }
        }
示例#3
0
        public void GetRuntimeEvents()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeEvents(null);
            });

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

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

                if (type.IsInterface)
                {
                    continue;
                }

                events.Clear();
                events.AddRange((IEnumerable <string>)type.GetDeclaredField("DeclaredEvents").GetValue(null));
                events.AddRange((IEnumerable <string>)type.GetDeclaredField("InheritedEvents").GetValue(null));

                Assert.All(type.AsType().GetRuntimeEvents(), e => Assert.True(events.Remove(e.Name)));
                Assert.Empty(events);
            }
        }
示例#4
0
        public Hybrid(IWebBrowserWrapper browser, object module)
        {
            this._Browser          = browser;
            _Browser.ScriptNotify += browser_ScriptNotify;

            this._Module = module;

            foreach (var method in RuntimeReflectionExtensions.GetRuntimeMethods(module.GetType()))
            {
                if ("webViewSaid" == method.Name)
                {
                    var args = method.GetParameters();

                    if (args.Length == 2)
                    {
                        _WebViewSaid = method;
                        break;
                    }
                }
            }

            if (_WebViewSaid == null)
            {
                throw new InvalidOperationException("module must have a webViewSaid(string, string) method");
            }
        }
        public void GetRuntimeProperties()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeProperties(null);
            });

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

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

                properties.Clear();
                properties.AddRange((IEnumerable <String>)type.GetDeclaredField("DeclaredPropertyNames").GetValue(null));
                properties.AddRange((IEnumerable <String>)type.GetDeclaredField("InheritedPropertyNames").GetValue(null));

                Assert.All(type.AsType().GetRuntimeProperties(), p => Assert.True(properties.Remove(p.Name)));
                Assert.Empty(properties);
            }
        }
示例#6
0
        public async Task <List <KeyValuePair <TMapperOutputKey, TMapperOutputValue> > > RunTasks <TMapperOutputKey, TMapperOutputValue>()
        {
            await Task.WhenAll((IEnumerable <Task>) MapperTasks);

            // concat all keyvalue pairs
            var allKeyValuePairsForNode = new List <List <KeyValuePair <TMapperOutputKey, TMapperOutputValue> > >();

            foreach (var mapperTask in MapperTasks)
            {
                var resultProperty = RuntimeReflectionExtensions.GetRuntimeProperty(mapperTask.GetType(), "Result").GetMethod;
                var result         = (List <KeyValuePair <TMapperOutputKey, TMapperOutputValue> >)resultProperty.Invoke(mapperTask, new object[] { });
                allKeyValuePairsForNode.Add(result);
            }
            var flatternList = allKeyValuePairsForNode.SelectMany(x => x.ToList()).ToList();

            if (_configurator.TypeOfCombiner != null)
            {
                var combiner      = (IReducer)Activator.CreateInstance((Type)_configurator.TypeOfCombiner);
                var combineMethod = RuntimeReflectionExtensions.GetRuntimeMethods(_configurator.TypeOfCombiner).Single(m => m.Name == "Combine" && m.IsPublic && m.GetParameters().Any());

                var combineTask = (Task <List <KeyValuePair <TMapperOutputKey, TMapperOutputValue> > >)combineMethod.Invoke(combiner, new object[] { combiner.GetHashCode().ToString(), flatternList });
                return(combineTask.Result);
            }

            return(flatternList);
        }
        static public string GetStatusResponseResultForMethod(object result, Delegate method)
        {
            var      methodInfo = RuntimeReflectionExtensions.GetMethodInfo(method);
            Type     ret        = methodInfo.ReturnType;
            TypeCode returnType = Convert.GetTypeCode(ret);

            try
            {
                bool isObj = returnType == TypeCode.Object;    //Convert.GetTypeCode(returnType)
                if (isObj)
                {
                    if (result != null && Convert.GetTypeCode(result) == returnType)
                    {
                        return(GetRequestResponseSuccess());
                    }
                    return(GetRequestResponseFailure());
                }
                else
                {
                    if (Convert.GetTypeCode(result) == returnType)
                    {
                        if (returnType == TypeCode.Boolean)   //check the response value for boolean
                        {
                            return((bool)result ? GetRequestResponseSuccess() : GetRequestResponseFailure());
                        }
                        return(GetRequestResponseSuccess());
                    }
                    return(GetRequestResponseFailure());
                }
            }
            catch (Exception)
            {
                return(GetRequestResponseFailure());
            }
        }
示例#8
0
        public static PropertyInfo GetRuntimeProperty(Type source, string name)
        {
#if NET35 || NET40
            return(source.GetProperty(name));
#else
            return(RuntimeReflectionExtensions.GetRuntimeProperty(source, name));
#endif
        }
示例#9
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
        }
示例#10
0
        public static MethodInfo GetMethodInfo(Delegate source)
        {
#if NET35 || NET40
            return(source.Method);
#else
            return(RuntimeReflectionExtensions.GetMethodInfo(source));
#endif
        }
示例#11
0
        public static IEnumerable <MethodInfo> GetRuntimeMethods(this Type type)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeMethods(type));
#else
            return(type.GetMethods());
#endif
        }
示例#12
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
        }
示例#13
0
        public static MethodInfo GetMethodInfo(this Delegate func)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetMethodInfo(func));
#else
            return(func.Method);
#endif
        }
示例#14
0
        public static IEnumerable <PropertyInfo> GetRuntimeProperties(this Type type)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeProperties(type));
#else
            return(type.GetProperties());
#endif
        }
示例#15
0
        public static EventInfo GetRuntimeEvent(this Type type, string name)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeEvent(type, name));
#else
            return(type.GetEvent(name));
#endif
        }
示例#16
0
 private string getMethodName()
 {
     if (_methodName == null)
     {
         _methodName = RuntimeReflectionExtensions.GetMethodInfo(_origObject).Name;
     }
     return(_methodName);
 }
示例#17
0
        public static PropertyInfo GetRuntimeProperty(this Type type, string name)
        {
#if UNITY_METRO && !UNITY_EDITOR
            return(RuntimeReflectionExtensions.GetRuntimeProperty(type, name));
#else
            return(type.GetProperty(name));
#endif
        }
        public static MethodInfo GetMethodInfo(this Delegate delegateInstance)
        {
            if (delegateInstance == null)
            {
                throw new ArgumentNullException("delegateInstance");
            }

            return(RuntimeReflectionExtensions.GetMethodInfo(delegateInstance));
        }
示例#19
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();
            }
        public static PropertyInfo GetAnyProperty(this Type type, string name)
        {
            List <PropertyInfo> list = (from p in RuntimeReflectionExtensions.GetRuntimeProperties(type)
                                        where p.Name == name
                                        select p).ToList();

            if (list.Count > 1)
            {
                throw new AmbiguousMatchException();
            }
            return(list.SingleOrDefault());
        }
示例#21
0
 public void Swapped(object newInstance)
 {
     try
     {
         var s = RuntimeReflectionExtensions.GetRuntimeField(newInstance.GetType(), "HotSwapped");
         s.SetValue(newInstance, true);
     }
     catch (Exception e)
     {
         AudioEngine.Log(e.Message);
     }
 }
示例#22
0
        public void GetRuntimeMethods()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeMethods(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("DeclaredMethodNames").GetValue(null));
                methods.AddRange((IEnumerable <String>)type.GetDeclaredField("InheritedMethodNames").GetValue(null));
                if (type.GetDeclaredField("NewMethodNames") != null)
                {
                    methods.AddRange((IEnumerable <String>)type.GetDeclaredField("NewMethodNames").GetValue(null));
                }

                //inherited from object
                methods.Add("System.String ToString()");
                methods.Add("Boolean Equals(System.Object)");
                methods.Add("Int32 GetHashCode()");
                methods.Add("System.Type GetType()");

                methods.Add("Void Finalize()");
                methods.Add("System.Object MemberwiseClone()");

                foreach (MethodInfo mi in type.AsType().GetRuntimeMethods())
                {
                    if (methods.Remove(mi.ToString()))
                    {
                        continue;
                    }

                    Assert.False(true, String.Format("Type: {0}, Method: {1} is not expected", type, mi));
                }

                foreach (String methodName in methods)
                {
                    Assert.False(true, String.Format("Method: {0} cannot be found", methodName));
                }
            }
        }
示例#23
0
        public void GetRuntimeProperty()
        {
            var types = GetTypes();

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

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

            Assert.Null(typeof(TestType).GetRuntimeProperty(""));

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

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

                properties.Clear();
                properties.AddRange((IEnumerable <string>)type.GetDeclaredField("PublicPropertyNames").GetValue(null));

                foreach (string propertyName in properties)
                {
                    bool exceptionExpected = propertyName.Equals("Item");

                    // Slight duplication of code her to allow use of Assert.Throws
                    if (exceptionExpected == true)
                    {
                        Assert.Throws <AmbiguousMatchException>(() =>
                        {
                            PropertyInfo pi = type.AsType().GetRuntimeProperty(propertyName);
                        });
                    }
                    else
                    {
                        PropertyInfo pi = type.AsType().GetRuntimeProperty(propertyName);
                        Assert.NotNull(pi);
                    }
                }
            }

            Assert.Equal(typeof(TestType).GetProperty("Length"), typeof(TestType).GetRuntimeProperty("Length"));
        }
示例#24
0
        public static void Calculate(ShowResult showResult, int a, int b)
        {
            if (a < 0 || b < 0)
            {
                Console.WriteLine("参数有误");
            }
            else
            {
                System.Reflection.MethodInfo methodInfo = RuntimeReflectionExtensions.GetMethodInfo(showResult);
                Console.WriteLine("在这里写入一条日志" + methodInfo.ToString());

                Console.WriteLine(showResult?.Invoke(a, b));
                //Console.WriteLine(showResult(a, b));
            }
        }
示例#25
0
        public static PropertyInfo[] GetPublicInstanceProperties(Type type)
        {
#if NETFX_CORE
            List <PropertyInfo> list = new List <PropertyInfo>();
            foreach (PropertyInfo propertyInfo in RuntimeReflectionExtensions.GetRuntimeProperties(type))
            {
                if (propertyInfo.GetMethod.IsPublic && !propertyInfo.GetMethod.IsStatic)
                {
                    list.Add(propertyInfo);
                }
            }
            return(list.ToArray());
#else
            return(type.GetProperties());
#endif
        }
示例#26
0
        public MvxDBMapping(Type type)
        {
            Type      = type;
            TableName = type.Name;
            var attributes = type.GetCustomAttributes(typeof(MvxDBTableAttribute), true).Cast <MvxDBTableAttribute>().ToList();

            if (attributes.Any())
            {
                TableName = attributes[0].Name;
            }

            var query = RuntimeReflectionExtensions.GetRuntimeProperties(type);

            PropertyInfoKey = query.FirstOrDefault(p => p.GetCustomAttribute <MvxDBKeyAttribute>(true) != null);
            query           = query.Where(p => p.CanRead && p.CanWrite && p.GetCustomAttribute <MvxDBIgnoreAttribute>(true) == null && p.GetCustomAttribute <MvxDBKeyAttribute>(true) == null);
            PropertiesInfos = query.ToList();
        }
示例#27
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>()));
        }
 public static IEnumerable <MemberInfo> GetMembersInHierarchy(this Type type)
 {
     do
     {
         foreach (PropertyInfo item in from pi in RuntimeReflectionExtensions.GetRuntimeProperties(type)
                  where !(pi.GetMethod ?? pi.SetMethod).IsStatic
                  select pi)
         {
             yield return(item);
         }
         foreach (FieldInfo item2 in from f in RuntimeReflectionExtensions.GetRuntimeFields(type)
                  where !f.IsStatic
                  select f)
         {
             yield return(item2);
         }
         type = type.BaseType;
     }while (type != null);
 }
        public void GetRuntimeEvents()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeEvents(null);
            });

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

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

                if (type.IsInterface)
                {
                    continue;
                }

                events.Clear();
                events.AddRange((IEnumerable <String>)type.GetDeclaredField("DeclaredEvents").GetValue(null));
                events.AddRange((IEnumerable <String>)type.GetDeclaredField("InheritedEvents").GetValue(null));

                foreach (EventInfo ei in type.AsType().GetRuntimeEvents())
                {
                    if (events.Remove(ei.Name))
                    {
                        continue;
                    }

                    Assert.False(true, String.Format("Type: {0}, Event: {1} is not expected", type, ei));
                }

                foreach (String eventName in events)
                {
                    Assert.False(true, String.Format("Event: {0} cannot be found", eventName));
                }
            }
        }
示例#30
0
        public void GetRuntimeFields()
        {
            var types = GetTypes();

            Assert.Throws <ArgumentNullException>(() =>
            {
                RuntimeReflectionExtensions.GetRuntimeFields(null);
            });

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

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

                fields.Clear();
                fields.AddRange((IEnumerable <String>)type.GetDeclaredField("DeclaredFieldNames").GetValue(null));
                fields.AddRange((IEnumerable <String>)type.GetDeclaredField("InheritedFieldNames").GetValue(null));
                if (type.GetDeclaredField("NewFieldNames") != null)
                {
                    fields.AddRange((IEnumerable <String>)type.GetDeclaredField("NewFieldNames").GetValue(null));
                }

                foreach (FieldInfo fi in type.AsType().GetRuntimeFields())
                {
                    if (fields.Remove(fi.Name))
                    {
                        continue;
                    }

                    Assert.False(true, String.Format("Type: {0}, Field: {1} is not expected", type, fi));
                }

                foreach (String fieldName in fields)
                {
                    Assert.False(true, String.Format("Field: {0} cannot be found", fieldName));
                }
            }
        }