//public static T Instance<T>() where T : class //{ // Func<Type, bool> func = null; // if (!UnityContainerExtensions.IsRegistered<T>(IFactory.iunityContainer_0)) // { // bool flag = false; // try // { // object obj; // Monitor.Enter(obj = IFactory.object_0, ref flag); // Assembly assembly = typeof(T).Assembly; // Class2 @class = Class2.smethod_0(assembly); // if (@class.method_0() != null) // { // foreach (ContainerRegistration current in @class.method_0().Registrations) // { // if (!UnityContainerExtensions.IsRegistered(IFactory.iunityContainer_0, current.RegisteredType)) // { // UnityContainerExtensions.RegisterType(IFactory.iunityContainer_0, current.RegisteredType, current.MappedToType, new InjectionMember[0]); // } // } // } // string bllSuffix = ".BLL"; // IEnumerable<Type> arg_DB_0 = assembly.GetTypes(); // if (func == null) // { // func = new Func<Type, bool>(IFactory.smethod_0<T>); // } // IEnumerable<Type> enumerable = from type in arg_DB_0.Where(func) // where type.BaseType != null && type.BaseType.IsGenericType && !type.IsInterface && typeof(T).IsAssignableFrom(type) && type.Namespace.EndsWith(bllSuffix) // select type; // foreach (Type current2 in enumerable) // { // UnityContainerExtensions.RegisterType(IFactory.iunityContainer_0, typeof(T), current2, new InjectionMember[0]); // } // } // finally // { // if (flag) // { // object obj=0; // Monitor.Exit(obj); // } // } // } // return UnityContainerExtensions.Resolve<T>(IFactory.iunityContainer_0, new ResolverOverride[0]); // Func<Type, bool> predicate = null; // if (!iunityContainer_0.IsRegistered<T>()) // { // lock (object_0) // { // Assembly assembly = typeof(T).Assembly; // Class2 class3 = Class2.smethod_0(assembly); // if (class3.method_0() != null) // { // foreach (ContainerRegistration registration in class3.method_0().Registrations) // { // if (!UnityContainerExtensions.IsRegistered(iunityContainer_0, registration.RegisteredType)) // { // UnityContainerExtensions.RegisterType(iunityContainer_0, registration.RegisteredType, registration.MappedToType, new InjectionMember[0]); // } // } // } // string bllSuffix = ".BLL"; // if (predicate == null) // { // predicate = new Func<Type, bool>(IFactory.smethod_0<T>); // } // foreach (Type type in from type in assembly.GetTypes().Where<Type>(predicate) // where (((type.BaseType != null) && type.BaseType.IsGenericType) && (!type.IsInterface && typeof(T).IsAssignableFrom(type))) && type.Namespace.EndsWith(bllSuffix) // select type) // { // UnityContainerExtensions.RegisterType(iunityContainer_0, typeof(T), type, new InjectionMember[0]); // } // } // } // return iunityContainer_0.Resolve<T>(new ResolverOverride[0]); //} public static T Instance <T>() where T : class { Func <Type, bool> predicate = null; if (!iunityContainer_0.IsRegistered <T>()) { lock (object_0) { Assembly assembly = typeof(T).Assembly; Class2 class3 = Class2.smethod_0(assembly); if (class3.method_0() != null) { foreach (ContainerRegistration registration in class3.method_0().Registrations) { if (!UnityContainerExtensions.IsRegistered(iunityContainer_0, registration.RegisteredType)) { UnityContainerExtensions.RegisterType(iunityContainer_0, registration.RegisteredType, registration.MappedToType, new InjectionMember[0]); } } } string bllSuffix = ".BLL"; if (predicate == null) { predicate = new Func <Type, bool>(IFactory.smethod_0 <T>); } foreach (Type type in from type in assembly.GetTypes().Where <Type>(predicate) where (((type.BaseType != null) && type.BaseType.IsGenericType) && (!type.IsInterface && typeof(T).IsAssignableFrom(type))) && type.Namespace.EndsWith(bllSuffix) select type) { UnityContainerExtensions.RegisterType(iunityContainer_0, typeof(T), type, new InjectionMember[0]); } } } return(iunityContainer_0.Resolve <T>(new ResolverOverride[0])); }
public bool IsRegistered(Type type, string name) { lock (this.container) { return(UnityContainerExtensions.IsRegistered(this.container, type, name)); } }
public object Resolve(Type t, string name) { object obj2; try { obj2 = ((t.IsClass && !t.IsAbstract) || UnityContainerExtensions.IsRegistered(this._container, t, name)) ? this._container.Resolve(t, name, new ResolverOverride[0]) : null; } catch (ResolutionFailedException exception) { throw new InvalidOperationException(string.Format("实例化类型失败,类型为:{0},名称为:{1}", t, name), (Exception)exception); } return(obj2); }
private void RegistrarMapeamento(Mapeamento mapeamento, bool sobreescrever) { if (string.IsNullOrEmpty(mapeamento.Nome)) { if (!sobreescrever && UnityContainerExtensions.IsRegistered(this._container, mapeamento.De)) { throw new Exception($"Já existe registro de mapeamento para este tipo ( {mapeamento.De.FullName} ), verifique o tipo ou faça o registro explicitando um nome."); } InjectionMember[] memberArray1 = new InjectionMember[] { new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <PolicyInjectionBehavior>() }; UnityContainerExtensions.RegisterType(this._container, mapeamento.De, mapeamento.Para, memberArray1); } else { if (!sobreescrever && UnityContainerExtensions.IsRegistered(this._container, mapeamento.De)) { throw new Exception($"Já existe registro de mapeamento nomeado para este tipo ( {mapeamento.De.FullName} ), verifique o tipo."); } InjectionMember[] memberArray2 = new InjectionMember[] { new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <PolicyInjectionBehavior>() }; UnityContainerExtensions.RegisterType(this._container, mapeamento.De, mapeamento.Para, mapeamento.Nome, memberArray2); } }
private void RegisterMapping(Mapping mapping, bool writeOver) { if (string.IsNullOrEmpty(mapping.Name)) { if (!writeOver && UnityContainerExtensions.IsRegistered(this._container, mapping.From)) { throw new Exception($"Já existe registro de mapeamento para este tipo ( {mapping.From.FullName} ), verifique o tipo ou faça o registro explicitando um nome."); } InjectionMember[] injectionMembers = new InjectionMember[] { new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <PolicyInjectionBehavior>() }; UnityContainerExtensions.RegisterType(this._container, mapping.From, mapping.To, injectionMembers); } else { if (!writeOver && UnityContainerExtensions.IsRegistered(this._container, mapping.From)) { throw new Exception($"Já existe registro de mapeamento nomeado para este tipo ( {mapping.From.FullName} ), verifique o tipo."); } InjectionMember[] injectionMembers = new InjectionMember[] { new Interceptor <InterfaceInterceptor>(), new InterceptionBehavior <PolicyInjectionBehavior>() }; UnityContainerExtensions.RegisterType(this._container, mapping.From, mapping.To, mapping.Name, injectionMembers); } }
/// <summary> /// 类型是否被注册到IoC容器 /// </summary> /// <param name="type"></param> /// <returns></returns> public bool IsRegistered(Type type) { return(UnityContainerExtensions.IsRegistered(this, type)); }