Ejemplo n.º 1
0
 private InjectionParameterValue InitializeParameterValue(PropertyInfo propInfo)
 {
     if(parameterValue == null)
     {
         parameterValue = new ResolvedParameter(propInfo.PropertyType);
     }
     return parameterValue;
 }
Ejemplo n.º 2
0
 private static void GuardPropertyValueIsCompatible(PropertyInfo property, InjectionParameterValue value)
 {
     if (!value.MatchesType(property.PropertyType))
     {
         throw new InvalidOperationException(
                   ExceptionMessage(Resources.PropertyTypeMismatch,
                                    property.Name,
                                    property.DeclaringType,
                                    property.PropertyType,
                                    value.ParameterTypeName));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Convert an arbitrary value to an InjectionParameterValue object. The rules are:
        /// If it's already an InjectionParameterValue, return it. If it's a Type, return a
        /// ResolvedParameter object for that type. Otherwise return an InjectionParameter
        /// object for that value.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>The resulting <see cref="InjectionParameterValue"/>.</returns>
        public static InjectionParameterValue ToParameter(object value)
        {
            InjectionParameterValue parameterValue = value as InjectionParameterValue;

            if (parameterValue != null)
            {
                return(parameterValue);
            }

            Type typeValue = value as Type;

            if (typeValue != null)
            {
                return(new ResolvedParameter(typeValue));
            }

            return(new InjectionParameter(value));
        }
        /// <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));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Construct a new <see cref="ResolvedArrayParameter"/> that
        /// resolves to the given array and element types and collection of element values.
        /// </summary>
        /// <param name="arrayParameterType">The type for the array of elements to resolve.</param>
        /// <param name="elementType">The type of elements to resolve.</param>
        /// <param name="elementValues">The values for the elements, that will
        /// be converted to <see cref="InjectionParameterValue"/> objects.</param>
        protected ResolvedArrayParameter(Type arrayParameterType, Type elementType, params object[] elementValues)
            : base(arrayParameterType)
        {
            Guard.ArgumentNotNull(elementType, "elementType");
            Guard.ArgumentNotNull(elementValues, "elementValues");

            this.elementType = elementType;
            this.elementValues.AddRange(InjectionParameterValue.ToParameters(elementValues));
            foreach (InjectionParameterValue pv in this.elementValues)
            {
                if (!pv.MatchesType(elementType))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  Resources.TypesAreNotAssignable,
                                  elementType,
                                  pv.ParameterTypeName));
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Constructs a new <see cref="ResourceInjectionValue"/> with an <see cref="InjectionParameterValue"/>.
 /// </summary>
 /// <param name="injectionParameterValue"><see cref="InjectionParameterValue"/> that resolves to a resource URI</param>
 /// <param name="many">true if there can be several resources; false if only one is expected</param>
 public ResourceInjectionValue(InjectionParameterValue injectionParameterValue, bool many = false)
     : base(many ? typeof(IList<IResource>) : typeof(IResource))
 {
     _injectionParameterValue = injectionParameterValue;
     _many = many;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Constructs a new <see cref="ResourceInjectionValue"/> with a string URI.
 /// </summary>
 /// <param name="uri">a string representing a resource</param>
 /// <param name="many">true if there can be several resources; false if only one is expected</param>
 public ResourceInjectionValue(string uri, bool many = false)
     : base(many ? typeof(IList<IResource>) : typeof(IResource))
 {
     _injectionParameterValue = new LateBindingInjectionValue<string>(uri);
     _many = many;
 }
Ejemplo n.º 8
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);
 }
 public SpecifiedConstructorSelectorPolicy(ConstructorInfo ctor, InjectionParameterValue[] parameterValues)
 {
     this.ctor = ctor;
     ctorReflector = new MethodReflectionHelper(ctor);
     this.parameterValues = parameterValues;
 }
 /// <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);
 }
Ejemplo n.º 11
0
 /// <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);
 }
 /// <summary>
 /// Create an instance of <see cref="SpecifiedConstructorSelectorPolicy"/> that
 /// will return the given constructor, being passed the given injection values
 /// as parameters.
 /// </summary>
 /// <param name="ctor">The constructor to call.</param>
 /// <param name="parameterValues">Set of <see cref="InjectionParameterValue"/> objects
 /// that describes how to obtain the values for the constructor parameters.</param>
 public SpecifiedConstructorSelectorPolicy(ConstructorInfo ctor, InjectionParameterValue[] parameterValues)
 {
     this.ctor = ctor;
     this.parameterValues = parameterValues;
 }
            protected override void VisitEnumerableParameterValue(ContainerResolvedEnumerableParameter parameterValue)
            {
                var resolveParameters = parameterValue.Names
                        .Select(name => new ResolvedParameter(parameterValue.ElementType, name))
                        .ToArray();

                InjectionParameter = new ResolvedArrayParameter(parameterValue.ElementType, resolveParameters);
            }
 protected override void VisitResolvedParameterValue(ContainerResolvedParameter parameterValue)
 {
     InjectionParameter = new ResolvedParameter(parameterValue.Type, parameterValue.Name);
 }
 protected override void VisitConstantParameterValue(ConstantParameterValue parameterValue)
 {
     this.InjectionParameter = new InjectionParameter(parameterValue.Type, parameterValue.Value);
 }
Ejemplo n.º 16
0
 /// <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);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Create a new <see cref="InjectionMethod"/> instance which will configure
 /// the container to call the given methods with the given parameters.
 /// </summary>
 /// <param name="methodName">Name of the method to call.</param>
 /// <param name="methodParameters">Parameter values for the method.</param>
 public InjectionMethod(string methodName, params object[] methodParameters)
 {
     this.methodName       = methodName;
     this.methodParameters = InjectionParameterValue.ToParameters(methodParameters).ToList();
 }
 private static void GuardPropertyValueIsCompatible(PropertyInfo property, InjectionParameterValue value)
 {
     if (!value.MatchesType(property.PropertyType))
      {
     throw new InvalidOperationException();
      }
 }
 /// <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 AutowireEnabledInjectionProperty(string propertyName, object propertyValue)
 {
     this.propertyName = propertyName;
      parameterValue = InjectionParameterValue.ToParameter(propertyValue);
 }
 /// <summary>
 /// Create a new instance of <see cref="InjectionConstructor"/> that looks
 /// for a constructor with the given set of parameters.
 /// </summary>
 /// <param name="parameterValues">The values for the parameters, that will
 /// be converted to <see cref="InjectionParameterValue"/> objects.</param>
 public InjectionConstructor(params object[] parameterValues)
 {
     this.parameterValues = Sequence.ToList(InjectionParameterValue.ToParameters(parameterValues));
 }
Ejemplo n.º 21
0
 private static void GuardPropertyValueIsCompatible(PropertyInfo property, InjectionParameterValue value)
 {
     if (!value.MatchesType(property.PropertyType))
     {
         throw new InvalidOperationException(
             ExceptionMessage(Resources.PropertyTypeMismatch,
                              property.Name,
                              property.DeclaringType,
                              property.PropertyType,
                              value.ParameterTypeName));
     }
 }
 /// <summary>
 /// Add a property that will be par of the set returned when the 
 /// <see cref="SelectProperties(IBuilderContext, IPolicyList)"/> is called.
 /// </summary>
 /// <param name="property">The property to set.</param>
 /// <param name="value"><see cref="InjectionParameterValue"/> object describing
 /// how to create the value to inject.</param>
 public void AddPropertyAndValue(PropertyInfo property, InjectionParameterValue value)
 {
     propertiesAndValues.Add(Pair.Make(property, value));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Create a new instance of <see cref="InjectionConstructor"/> that looks
 /// for a constructor with the given set of parameters.
 /// </summary>
 /// <param name="parameterValues">The values for the parameters, that will
 /// be converted to <see cref="InjectionParameterValue"/> objects.</param>
 public InjectionConstructorRelaxed(params object[] parameterValues)
 {
     _parameterValues = InjectionParameterValue.ToParameters(parameterValues).ToList();
 }
 /// <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);
 }
Ejemplo n.º 25
0
 ///<summary>
 /// Create an instance of <see cref="PropertyOverride"/>.
 ///</summary>
 ///<param name="propertyName">The property name.</param>
 ///<param name="propertyValue">Value to use for the property.</param>
 public PropertyOverride(string propertyName, object propertyValue)
 {
     this.propertyName = propertyName;
     this.propertyValue = InjectionParameterValue.ToParameter(propertyValue);
 }
 private InjectionParameterValue InitializeParameterValue(PropertyInfo propInfo)
 {
     return parameterValue ?? (parameterValue = new ResolvedParameter(propInfo.PropertyType));
 }