public virtual object Get(ICreationContext context, out bool newInstance) { Enforce.ArgumentNotNull(context, "context"); CheckNotDisposed(); lock (_synchRoot) { object instance; if (_scope.InstanceAvailable) { instance = _scope.GetInstance(); newInstance = false; } else { PreparingEventArgs preparingArgs = new PreparingEventArgs(context, this, _parameters); Preparing(this, preparingArgs); instance = preparingArgs.Instance ?? _activator.Create(context, preparingArgs.Parameters); ActivatingEventArgs activatingArgs = new ActivatingEventArgs(context, this, instance); Activating(this, activatingArgs); instance = activatingArgs.Instance; _scope.SetInstance(instance); newInstance = true; } return(instance); } }
private IClassFactory ChooseFactory(ICreationContext creationContext, Type[] parameterTypes) { if (parameterTypes.Length == 0) { if (noArgumentsFactory == null) { lock (configurationLock) { if (noArgumentsFactory == null) { noArgumentsFactory = creationContext.BuildFactory(ObjectType, Type.EmptyTypes); } } } return(noArgumentsFactory); } IClassFactory factory; if ((factory = TryGetFactory(parameterTypes)) == null) { lock (configurationLock) { if ((factory = TryGetFactory(parameterTypes)) == null) { if (factories == null) { factories = new ConcurrentDictionary <Type[], IClassFactory>(TypeArrayEqualityComparer.Instance); } factories[parameterTypes] = factory = creationContext.BuildFactory(ObjectType, parameterTypes); } } } return(factory); }
private IClassFactory ChooseFactory(ICreationContext creationContext, Type[] parameterTypes) { if (parameterTypes.Length == 0) { if (noArgumentsFactory == null) { lock (configurationLock) if (noArgumentsFactory == null) { noArgumentsFactory = creationContext.BuildFactory(ObjectType, Type.EmptyTypes); } } return(noArgumentsFactory); } var types = new TypeArray(parameterTypes); IClassFactory factory; if ((factory = TryGetFactory(types)) == null) { lock (configurationLock) if ((factory = TryGetFactory(types)) == null) { if (factories == null) { factories = new Hashtable(); } factories.Add(types, factory = creationContext.BuildFactory(ObjectType, parameterTypes)); } } return(factory); }
void invokeSetters(object instance, ICreationContext context) { Enforce.ArgumentNotNull(instance, "instance"); Enforce.ArgumentNotNull(context, "context"); if (null == _explicitPropertySetters) { return; } Type instanceType = instance.GetType(); foreach (PropertyInfo propertyInfo in instanceType.GetProperties( BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty)) { foreach (NamedPropertyParameter param in _explicitPropertySetters) { Func <object> propertyValueAccessor; if (param.TryGetProvider(propertyInfo.GetSetMethod().GetParameters()[0] , context, out propertyValueAccessor)) { propertyInfo.SetValue(instance, propertyValueAccessor(), null); } } } }
public object Get(IComponentRegistration registration) { ICreationContext context = null; bool newInstance; return(registration.Get(context, out newInstance)); }
bool CanUseConstructor(ConstructorInfo ci, ICreationContext context, IEnumerable <IParameter> parameters, out Func <object>[] valueAccessors, out string reason) { Enforce.ArgumentNotNull(ci, "ci"); Enforce.ArgumentNotNull(context, "context"); Enforce.ArgumentNotNull(parameters, "parameters"); StringBuilder reasonNotUsable = null; ParameterInfo[] ciParams = ci.GetParameters(); Func <object>[] partialValueAccessors = new Func <object> [ciParams.Length]; foreach (ParameterInfo pi in ciParams) { Func <object> va = null; foreach (IParameter param in parameters) { if (param.TryGetProvider(pi, context, out va)) { break; } } if (va != null) { partialValueAccessors[pi.Position] = va; } else { if (reasonNotUsable == null) { reasonNotUsable = new StringBuilder(); reasonNotUsable.Append(ci).Append(": "); } else { reasonNotUsable.Append(", "); } reasonNotUsable.AppendFormat( "类型为 '{0}' 的参数 '{1}' 没有找到", pi.ParameterType, pi.Name); } } if (reasonNotUsable != null) { valueAccessors = null; reason = reasonNotUsable.Append('.').ToString(); } else { valueAccessors = partialValueAccessors; reason = String.Empty; } return(reasonNotUsable == null); }
public T ToUnique <T>(ICreationContext store) where T : class { if (store.AlreadyCreated(ORID)) { return((T)store.GetExistingObject(ORID)); } T genericObject = (T)store.CreateObject(OClassName); var result = ToObject(genericObject, ""); store.AddObject(ORID, result); return(result); }
object invokeConstructors(ConstructorInfo ci, ICreationContext context, Func <object>[] parameterAccessors) { Enforce.ArgumentNotNull(ci, "ci"); Enforce.ArgumentNotNull(parameterAccessors, "parameterAccessors"); object[] parameters = new object[parameterAccessors.Length]; for (int i = 0; i < parameters.Length; ++i) { parameters[i] = parameterAccessors[i](); } return(_constructorInfo.Invoke(parameters)); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { if (instance == null) { lock (configurationLock) if (instance == null) { IClassFactory noParametersFactory = implementation.GetFactory(Type.EmptyTypes, creationContext); return(instance = noParametersFactory.Create(context, new object[0])); } } context.Reused(ObjectType); return(instance); }
public bool TryGetProvider(System.Reflection.ParameterInfo pi, ICreationContext context, out Func <object> valueProvider) { IComponentRegistration registration; if (context.TryGetRegistered(pi.ParameterType, out registration)) { valueProvider = delegate() { return(MatchTypes(pi, context.Get(registration))); }; return(true); } else { valueProvider = null; return(false); } }
public bool TryGetProvider(ParameterInfo pi, ICreationContext context, out Func <object> valueProvider) { Enforce.ArgumentNotNull(pi, "pi"); if (_predicate(pi)) { valueProvider = delegate() { return(MatchTypes(pi, _value)); }; return(true); } else { valueProvider = null; return(false); } }
public object Create(ICreationContext context, IEnumerable <IParameter> parameters) { Enforce.ArgumentNotNull(context, "context"); Enforce.ArgumentNotNull(parameters, "parameters"); IEnumerable <IParameter> argumentedParameters = Concat(parameters, _additionalConstructorParameters); Func <object>[] valueProviders = null; ConstructorInfo selectedCI = GetConstructorInfo(context, argumentedParameters, out valueProviders); object result = invokeConstructors(selectedCI, context, valueProviders); invokeSetters(result, context); return(result); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { if (instance == null) { lock (configurationLock) { if (instance == null) { var noParametersFactory = implementation.GetFactory(Type.EmptyTypes, creationContext); instance = noParametersFactory.Create(context, EmptyArray <object> .Instance); InstanceCreationOrder = InstanceCreationOrderProvider.Next; return(instance); } } } context.Reused(ObjectType); return(instance); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { throw new ForbiddenAbstractionException(abstractionType); }
public IClassFactory GetFactory(Type[] parameterTypes, ICreationContext creationContext) { throw new NotSupportedException(); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { context.Reused(typeof(IContainer)); return(context.Container); }
public object GetOrCreateInstance(IInjectionContext context, ICreationContext creationContext) { context.Reused(ObjectType); return(instance); }
public IClassFactory GetFactory(Type[] parameterTypes, ICreationContext creationContext) { IClassFactory classFactory = implementation.GetFactory(parameterTypes, creationContext); return(classFactory); }
public ConstructorInfo GetConstructorInfo(ICreationContext context, IEnumerable <IParameter> parameters, out Func <object>[] valueProviders) { if (null == _constructorInfo) { ParameterInfo[] parameterInfos = _constructorInfo.GetParameters(); valueProviders = new Func <object> [parameterInfos.Length]; foreach (ParameterInfo parameterInfo in parameterInfos) { Func <object> va = null; foreach (IParameter param in parameters) { if (param.TryGetProvider(parameterInfo, context, out va)) { break; } } if (null == va) { throw new DependencyResolutionException( string.Format("类型 '{0}' 没有找到组件:{1}" , _componentType, parameterInfo.Name)); } valueProviders[parameterInfo.Position] = va; } return(_constructorInfo); } List <KeyValuePair <ConstructorInfo, Func <object>[]> > possibleConstructors = new List <KeyValuePair <ConstructorInfo, Func <object>[]> >(); bool foundPublicConstructor = false; StringBuilder reasons = null; foreach (ConstructorInfo ci in _componentType.FindMembers( MemberTypes.Constructor, BindingFlags.Instance | BindingFlags.Public, null, null)) { foundPublicConstructor = true; Func <object>[] parameterAccessors; string reason; if (CanUseConstructor(ci, context, parameters, out parameterAccessors, out reason)) { possibleConstructors.Add(new KeyValuePair <ConstructorInfo, Func <object>[]>(ci, parameterAccessors)); } else { reasons = reasons ?? new StringBuilder(reason.Length + 2); reasons.AppendLine(); reasons.Append(reason); } } if (!foundPublicConstructor) { throw new DependencyResolutionException( string.Format("类型 '{0}' 没有公共构造函数.", _componentType)); } if (possibleConstructors.Count == 0) { throw new DependencyResolutionException( string.Format("类型 '{0}' 没有找到公共构造函数. 不合适的构造函数包括:{1}" , _componentType, reasons ?? new StringBuilder())); } valueProviders = null; ConstructorInfo result = null; foreach (KeyValuePair <ConstructorInfo, Func <object>[]> kp in possibleConstructors) { if (result == null || result.GetParameters().Length < kp.Key.GetParameters().Length) { result = kp.Key; valueProviders = null; } } return(result); }
public PreparingEventArgs(ICreationContext context, IComponentRegistration component, IEnumerable <IParameter> parameters) { Context = context; Component = component; Parameters = parameters; }
public object Create(ICreationContext context, IEnumerable <IParameter> parameters) { return(_instance); }
public IClassFactory GetFactory(Type[] parameterTypes, ICreationContext creationContext) { IClassFactory classFactory = ChooseFactory(creationContext, parameterTypes); return(classFactory); }
public virtual void InstanceActivated(ICreationContext context, object instance) { ActivatedEventArgs activatedArgs = new ActivatedEventArgs(context, this, instance); Activated(this, activatedArgs); }
public ActivatedEventArgs(ICreationContext context, IComponentRegistration component, object instance) { Context = context; Component = component; Instance = instance; }
public IClassFactory GetFactory(Type[] parameterTypes, ICreationContext creationContext) { throw new ForbiddenAbstractionException(abstractionType); }