コード例 #1
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));
     }
 }
コード例 #2
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));
        }
コード例 #3
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));
                }
            }
        }
コード例 #4
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);
 }
コード例 #5
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();
 }
コード例 #6
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);
 }
コード例 #7
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);
 }