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> /// 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> /// 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)); } } }
/// <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> /// 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); }
/// <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 = InjectionParameterValue.ToParameters(parameterValues).ToList(); }
/// <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(); }
/// <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); }
/// <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)); }
/// <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); }