public void Add(ParameterMatchingConvention convention) { Guard.AssertNotNull(convention, "convention"); IParameterMatchingConventionsPolicy policy = this.Context.Policies.Get <IParameterMatchingConventionsPolicy>(null); if (policy != null) { policy.Add(convention); } }
public ParameterMatcher(Type typeToCreate, IEnumerable <ConstructorParameter> constructorArguments, IParameterMatchingConventionsPolicy conventions) { Guard.AssertNotNull(constructorArguments, "ctorArgs"); Guard.AssertNotNull(conventions, "conventions"); this._typeToCreate = typeToCreate; this.constructorArguments = constructorArguments; this.conventions = conventions; this.filters = new CompositePredicate <ConstructorInfo>(); this.filters += this.ConstructorDoesNotTakeAllArguments; this.filters += this.NonSatisfiedPrimitiveArgs; }
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies) { Guard.AssertNotNull(implementationType, "implementationType"); Guard.AssertNotNull(policies, "policies"); ConstructorInfo ctor; InjectionParameterValue[] parameterValues; if (this.parameters.Count == 0) { ctor = implementationType.GetConstructor(Type.EmptyTypes); parameterValues = new InjectionParameterValue[0]; } else { NamedTypeBuildKey key = new NamedTypeBuildKey(implementationType, name); IParameterMatchingConventionsPolicy conventions = policies.Get <IParameterMatchingConventionsPolicy>(key); if (conventions == null) { conventions = new DefaultMatchingConventionsPolicy(); policies.SetDefault <IParameterMatchingConventionsPolicy>(conventions); } ctor = FindConstructor(implementationType, this.parameters, conventions); parameterValues = GetParameterValues(ctor, this.parameters, conventions); } IConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(ctor, parameterValues); NamedTypeBuildKey buildKey = new NamedTypeBuildKey(implementationType, name); policies.Set <IConstructorSelectorPolicy>(policy, buildKey); }
private static InjectionParameterValue[] GetParameterValues(ConstructorInfo ctor, IEnumerable <ConstructorParameter> constructorArguments, IParameterMatchingConventionsPolicy conventions) { ParameterInfo[] parameters = ctor.GetParameters(); object[] parameterValues = new object[parameters.Length]; // fill in the provided values and add the type of the parameter that the // ctor expects otherwise. unity will take care of resolving parameters that // are provided that way for (int i = 0; i < parameters.Length; i++) { ConstructorParameter argument = constructorArguments.FirstOrDefault(a => conventions.Matches(a, parameters[i])); if (argument != null) { Type parameterType = parameters[i].ParameterType; if (argument.Value is string && parameterType != typeof(string) && (parameterType.IsInterface || parameterType.IsClass)) { parameterValues[i] = new ResolvedParameter(parameterType, (string)argument.Value); } else { parameterValues[i] = argument.Value; } } else { parameterValues[i] = parameters[i].ParameterType; } } return(InjectionParameterValue.ToParameters(parameterValues).ToArray()); }
private static ConstructorInfo FindConstructor(Type typeToCreate, IEnumerable <ConstructorParameter> constructorArguments, IParameterMatchingConventionsPolicy conventions) { ConstructorInfo[] ctors = typeToCreate.GetConstructors(); ParameterMatcher matcher = new ParameterMatcher(typeToCreate, constructorArguments, conventions); return(matcher.BestMatch(ctors)); }