private static void RegisterType(Type type, string lifeCycle) { var exportAttributes = type.GetCustomAttributes(typeof(ExportAttribute), false); if (exportAttributes == null || exportAttributes.Length == 0) { return; } ExportAttribute exportAttribute = exportAttributes[0] as ExportAttribute; //string[] filterArray = new string[] { "IEnumerable" }; //Type[] objectInterfaces = interfaces.Where(pre => !filterArray.Contains(pre.Name) && !(pre.IsGenericType)).ToArray(); //反射类型中包含的所有非系统的方法,判断方法中是否带有特性,如果参数中不包含特性,则不处理 var allMethods = exportAttribute.InterfaceType.GetMethods(System.Reflection.BindingFlags.Public); if (methodMappingContainer.ContainsKey(type)) { IList <ValidatorMethodMapping> methodMappings = methodMappingContainer[type]; //添加当前方法对应的映射元数据信息 foreach (var method in allMethods) { ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method); if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0) { methodMappings.Add(methodMapping); } } } else { IList <ValidatorMethodMapping> methodMappings = new List <ValidatorMethodMapping>(); foreach (var method in allMethods) { ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method); if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0) { methodMappings.Add(methodMapping); } } methodMappingContainer.Add(type, methodMappings); } }
public void RegisterType(Type type) { //string[] filterArray = new string[] { "IEnumerable" }; //Type[] objectInterfaces = interfaces.Where(pre => !filterArray.Contains(pre.Name) && !(pre.IsGenericType)).ToArray(); //反射类型中包含的所有非系统的方法,判断方法中是否带有特性,如果参数中不包含特性,则不处理 var allMethods = type.GetMethods(); if (allMethods.Length == 0) { return; } if (methodMappingContainer.ContainsKey(type)) { IList <ValidatorMethodMapping> methodMappings = methodMappingContainer[type]; //添加当前方法对应的映射元数据信息 foreach (var method in allMethods) { ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method); if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0) { methodMappings.Add(methodMapping); } } } else { IList <ValidatorMethodMapping> methodMappings = new List <ValidatorMethodMapping>(); foreach (var method in allMethods) { ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method); if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0) { methodMappings.Add(methodMapping); } } if (methodMappings.Count > 0) { methodMappingContainer.Add(type, methodMappings); } } }
public object CreateInstance(System.Reflection.MethodInfo methodInfo, System.Reflection.ParameterInfo parmeterInfo) { lock (lock_flag) { ValidatorMethodMapping mapping = new ValidatorMethodMapping(methodInfo); if (!methodMappingContainer.ContainsKey(methodInfo.DeclaringType)) { return(null); } var mappings = methodMappingContainer[methodInfo.DeclaringType]; var methodMapping = mappings.Where(pre => pre == mapping).FirstOrDefault(); if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0) { var parameterMapping = methodMapping.MethodParameters.Where(pre => pre.ParameterInfo.ParameterType == parmeterInfo.ParameterType && pre.ParameterInfo.Position == parmeterInfo.Position).FirstOrDefault(); if (parameterMapping == null || parameterMapping.ValidatorType == null) { return(null); } //如果不包含则处理所有的内容 if (!validatorInstanceTypeContainer.Contains(parameterMapping.ValidatorType)) { ObjectContainer.RegisterInstance(parameterMapping.ValidatorType, ObjectContainer.CreateInstance(parameterMapping.ValidatorType)); } return(ObjectContainer.CreateInstance(parameterMapping.ValidatorType)); } return(null); } }