/// <summary>
        /// Add policies to the <paramref name="policies"/> to configure the
        /// container to call this constructor with the appropriate parameter values.
        /// </summary>
        /// <param name="serviceType">Interface registered, ignored in this implementation.</param>
        /// <param name="implementationType">Type to register.</param>
        /// <param name="name">Name used to resolve the type object.</param>
        /// <param name="policies">Policy list to add policies to.</param>
        public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
        {
            ConstructorInfo ctor = FindExactMatchingConstructor(implementationType);

            if (ctor == null)
            {
                //if exact matching ctor not found, use the longest one and try to adjust the parameters.
                //use given Params if type matches otherwise use the type to advise Unity to resolve later
                ctor = FindLongestConstructor(implementationType);
                if (ctor != null)
                {
                    //adjust parameters
                    var newParams = new List <InjectionParameterValue>();
                    foreach (var parameter in ctor.GetParameters())
                    {
                        var injectionParameterValue =
                            _parameterValues.FirstOrDefault(value => value.MatchesType(parameter.ParameterType));
                        if (injectionParameterValue != null)
                        {
                            newParams.Add(injectionParameterValue);
                            _parameterValues.Remove(injectionParameterValue);
                        }
                        else
                        {
                            newParams.Add(InjectionParameterValue.ToParameter(parameter.ParameterType));
                        }
                    }
                    _parameterValues = newParams;
                }
                else
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  "No constructor found for type {0}.",
                                  implementationType.GetTypeInfo().Name));
                }
            }
            policies.Set <IConstructorSelectorPolicy>(
                new SpecifiedConstructorSelectorPolicy(ctor, _parameterValues.ToArray()),
                new NamedTypeBuildKey(implementationType, name));
        }
 /// <summary>
 /// Create an instance of <see cref="DependencyOverride"/> to override
 /// the given type with the given value.
 /// </summary>
 /// <param name="typeToConstruct">Type of the dependency.</param>
 /// <param name="dependencyValue">Value to use.</param>
 public DependencyOverride(Type typeToConstruct, object dependencyValue)
 {
     this.typeToConstruct = typeToConstruct;
     this.dependencyValue = InjectionParameterValue.ToParameter(dependencyValue);
 }
示例#3
0
 /// <summary>
 /// Configure the container to inject the given property name,
 /// using the value supplied. This value is converted to an
 /// <see cref="InjectionParameterValue"/> object using the
 /// rules defined by the <see cref="InjectionParameterValue.ToParameters"/>
 /// method.
 /// </summary>
 /// <param name="propertyName">Name of property to inject.</param>
 /// <param name="propertyValue">Value for property.</param>
 public InjectionProperty(string propertyName, object propertyValue)
 {
     this.propertyName   = propertyName;
     this.parameterValue = InjectionParameterValue.ToParameter(propertyValue);
 }
 /// <summary>
 /// Construct a new <see cref="ParameterOverride"/> object that will
 /// override the given named constructor parameter, and pass the given
 /// value.
 /// </summary>
 /// <param name="parameterName">Name of the constructor parameter.</param>
 /// <param name="parameterValue">Value to pass for the constructor.</param>
 public ParameterOverride(string parameterName, object parameterValue)
 {
     this.parameterName  = parameterName;
     this.parameterValue = InjectionParameterValue.ToParameter(parameterValue);
 }