/// <summary> /// Initialize<br/> /// 初始化<br/> /// </summary> public ContainerFactoryData( ContainerFactoryDelegate factoryFunc, ReuseType reuseType, Type implementationTypeHint) { _factoryFunc = factoryFunc; _factoryLock = new object(); _isGenericTypeDefinition = implementationTypeHint.IsGenericTypeDefinition; if (reuseType == ReuseType.Singleton) { if (_isGenericTypeDefinition) { _singletonInstance = new ConcurrentDictionary <string, object>(); } } else if (reuseType == ReuseType.Scoped) { _scopedInstance = new AsyncLocal <object>(); } else if (reuseType != ReuseType.Transient) { throw new NotSupportedException(string.Format( "Unsupported reuse type {0}", ReuseType)); } ReuseType = reuseType; ImplementationTypeHint = implementationTypeHint; }
/// <summary> /// Register delegate with service type and service key<br/> /// 根据服务类型和服务键注册工厂函数<br/> /// </summary> public void RegisterDelegate( Type serviceType, ContainerFactoryDelegate factory, ReuseType reuseType = ReuseType.Transient, object serviceKey = null) { // Since we can't figure out what type will returned from factory, // the implementation hint type will be the service type var factoryData = new ContainerFactoryData( factory, reuseType, serviceType); RegisterFactory(serviceType, factoryData, serviceKey); }