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); } }
internal static void RegisterType(Type type, string lifeCycle) { var exportAttributes = type.GetCustomAttributes(typeof(EventExportAttribute), false); if (exportAttributes == null || exportAttributes.Length == 0) { return; } EventExportAttribute exportAttribute = exportAttributes[0] as EventExportAttribute; if (exportAttribute.EventType == null) { return; } var eventType = exportAttribute.EventType; switch (GetObjectLifeCycle(lifeCycle)) { case ObjectLifeCycle.Singleton: ObjectContainer.RegisterInstance(exportAttribute.InterfaceType, Activator.CreateInstance(type)); if (eventHandlerContainer.ContainsKey(eventType)) { eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair() { InterfaceType = exportAttribute.InterfaceType, ImplementType = type }); } else { IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>(); tempTypes.Add(new EventHandlerKeyValuePair() { InterfaceType = exportAttribute.InterfaceType, ImplementType = type }); eventHandlerContainer.Add(eventType, tempTypes); } break; case ObjectLifeCycle.New: if (eventHandlerContainer.ContainsKey(eventType)) { if (eventHandlerContainer[eventType].Where(pre => pre.ImplementType == type).Count() > 0) { return; } eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair() { InterfaceType = exportAttribute.InterfaceType, ImplementType = type }); } else { IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>(); tempTypes.Add(new EventHandlerKeyValuePair() { InterfaceType = exportAttribute.InterfaceType, ImplementType = type }); eventHandlerContainer.Add(eventType, tempTypes); } ObjectContainer.RegisterType(exportAttribute.InterfaceType, type); ObjectContainer.RegisterType(type, type); break; } }