/// <summary>
 ///   Return the sequence of methods to call while building the target object.
 /// </summary>
 /// <param name = "context">Current build context.</param>
 /// <param name = "resolverPolicyDestination">The <see cref = 'IPolicyList' /> to add any
 ///   generated resolver objects into.</param>
 /// <returns>Sequence of methods to call.</returns>
 public IEnumerable<SelectedMethod> SelectMethods(IBuilderContext context, IPolicyList resolverPolicyDestination)
 {
     foreach (Pair<MethodInfo, IEnumerable<InjectionParameterValue>> method in methods)
     {
         Type typeToBuild = context.BuildKey.Type;
         SelectedMethod selectedMethod;
         var typeReflector = new ReflectionHelper(method.First.DeclaringType);
         var methodReflector = new MethodReflectionHelper(method.First);
         if (!methodReflector.MethodHasOpenGenericParameters && !typeReflector.IsOpenGeneric)
         {
             selectedMethod = new SelectedMethod(method.First);
         }
         else
         {
             Type[] closedMethodParameterTypes =
                 methodReflector.GetClosedParameterTypes(typeToBuild.GetGenericArguments());
             selectedMethod = new SelectedMethod(
                 typeToBuild.GetMethod(method.First.Name, closedMethodParameterTypes));
         }
         SpecifiedMemberSelectorHelper.AddParameterResolvers(
             typeToBuild,
             resolverPolicyDestination,
             method.Second,
             selectedMethod);
         yield return selectedMethod;
     }
 }
Example #2
0
        public override bool MatchesType(Type t)
        {
            Guard.ArgumentNotNull(t, "t");
            ReflectionHelper candidateReflector = new ReflectionHelper(t);
            if (candidateReflector.IsOpenGeneric && this.parameterReflector.IsOpenGeneric)
            {
                return candidateReflector.Type.GetGenericTypeDefinition() ==
                       this.parameterReflector.Type.GetGenericTypeDefinition();
            }

            return t.GetTypeInfo().IsAssignableFrom(this.parameterReflector.Type.GetTypeInfo());
        }
        /// <summary>
        ///   Return a <see cref = "IDependencyResolverPolicy" /> instance that will
        ///   return this types value for the parameter.
        /// </summary>
        /// <param name = "typeToBuild">Type that contains the member that needs this parameter. Used
        ///   to resolve open generic parameters.</param>
        /// <returns>The <see cref = "IDependencyResolverPolicy" />.</returns>
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            Guard.ArgumentNotNull(typeToBuild, "typeToBuild");
            var parameterReflector = new ReflectionHelper(ParameterType);

            Type typeToResolve = parameterReflector.Type;
            if (parameterReflector.IsOpenGeneric)
            {
                typeToResolve = parameterReflector.GetClosedParameterType(typeToBuild.GetGenericArguments());
            }

            return new OptionalDependencyResolverPolicy(typeToResolve, name);
        }
        /// <summary>
        /// Return a <see cref="IDependencyResolverPolicy"/> instance that will
        /// return this types value for the parameter.
        /// </summary>
        /// <param name="typeToBuild">Type that contains the member that needs this parameter. Used
        /// to resolve open generic parameters.</param>
        /// <returns>The <see cref="IDependencyResolverPolicy"/>.</returns>
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            this.GuardTypeToBuildIsGeneric(typeToBuild);
            this.GuardTypeToBuildHasMatchingGenericParameter(typeToBuild);

            Type typeToResolve = new ReflectionHelper(typeToBuild).GetNamedGenericParameter(this.genericParameterName);

            var resolverPolicies = new List<IDependencyResolverPolicy>();
            foreach (InjectionParameterValue pv in this.elementValues)
            {
                resolverPolicies.Add(pv.GetResolverPolicy(typeToBuild));
            }
            return new ResolvedArrayWithElementsResolverPolicy(typeToResolve, resolverPolicies.ToArray());
        }
Example #5
0
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            Guard.ArgumentNotNull(typeToBuild, "typeToBuild");

            var parameterReflector = new ReflectionHelper(ParameterType);

            if(parameterReflector.IsGenericArray)
            {
                return CreateGenericArrayResolverPolicy(typeToBuild, parameterReflector);
            }

            if (parameterReflector.IsOpenGeneric)
            {
                return CreateGenericResolverPolicy(typeToBuild, parameterReflector);
            }

            return CreateResolverPolicy(parameterReflector.Type);
        }
        /// <summary>
        /// Returns sequence of properties on the given type that
        /// should be set as part of building that object.
        /// </summary>
        /// <param name="context">Current build context.</param>
        /// <param name="resolverPolicyDestination">The <see cref='IPolicyList'/> to add any
        /// generated resolver objects into.</param>
        /// <returns>Sequence of <see cref="PropertyInfo"/> objects
        /// that contain the properties to set.</returns>
        public IEnumerable<SelectedProperty> SelectProperties(IBuilderContext context, IPolicyList resolverPolicyDestination)
        {
            Type typeToBuild = context.BuildKey.Type;
            var currentTypeReflector = new ReflectionHelper(context.BuildKey.Type);
            foreach (Pair<PropertyInfo, InjectionParameterValue> pair in propertiesAndValues)
            {
                PropertyInfo currentProperty = pair.First;

                // Is this the property info on the open generic? If so, get the one
                // for the current closed generic.
                if (new ReflectionHelper(pair.First.DeclaringType).IsOpenGeneric)
                {
                    currentProperty = currentTypeReflector.Type.GetTypeInfo().GetDeclaredProperty(currentProperty.Name);
                }

                yield return new SelectedProperty(currentProperty, pair.Second.GetResolverPolicy(typeToBuild));
            }
        }
        /// <summary>
        /// Choose the constructor to call for the given type.
        /// </summary>
        /// <param name="context">Current build context</param>
        /// <param name="resolverPolicyDestination">The <see cref='IPolicyList'/> to add any
        /// generated resolver objects into.</param>
        /// <returns>The chosen constructor.</returns>
        public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPolicyDestination)
        {
            SelectedConstructor result;
            Type typeToBuild = context.BuildKey.Type;

            ReflectionHelper typeReflector = new ReflectionHelper(ctor.DeclaringType);
            if (!ctorReflector.MethodHasOpenGenericParameters && !typeReflector.IsOpenGeneric)
            {
                result = new SelectedConstructor(ctor);
            }
            else
            {
                Type[] closedCtorParameterTypes =
                    ctorReflector.GetClosedParameterTypes(typeToBuild.GetGenericArguments());
                result = new SelectedConstructor(typeToBuild.GetConstructor(closedCtorParameterTypes));
            }

            SpecifiedMemberSelectorHelper.AddParameterResolvers(typeToBuild, resolverPolicyDestination, parameterValues, result);
            return result;
        }
Example #8
0
        private ConstructorInfo FindConstructor(Type typeToCreate)
        {
            var matcher = new ParameterMatcher(this.parameterValues);
            var typeToCreateReflector = new ReflectionHelper(typeToCreate);

            foreach (ConstructorInfo ctor in typeToCreateReflector.InstanceConstructors)
            {
                if (matcher.Matches(ctor.GetParameters()))
                {
                    return ctor;
                }
            }

            string signature = string.Join(", ", this.parameterValues.Select(p => p.ParameterTypeName).ToArray());

            throw new InvalidOperationException(
                string.Format(CultureInfo.CurrentCulture,
                    Resources.NoSuchConstructor,
                    typeToCreate.FullName,
                    signature));
        }
Example #9
0
 private IDependencyResolverPolicy CreateGenericArrayResolverPolicy(Type typeToBuild, ReflectionHelper parameterReflector)
 {
     Type arrayType = parameterReflector.GetClosedParameterType(typeToBuild.GenericTypeArguments);
     return new NamedTypeDependencyResolverPolicy(arrayType, name);
 }
Example #10
0
 /// <summary>
 /// Create a new <see cref="TypedInjectionValue"/> that exposes
 /// information about the given <paramref name="parameterType"/>.
 /// </summary>
 /// <param name="parameterType">Type of the parameter.</param>
 protected TypedInjectionValue(Type parameterType)
 {
     this.parameterReflector = new ReflectionHelper(parameterType);
 }
 private IDependencyResolverPolicy CreateGenericResolverPolicy(Type typeToBuild,
     ReflectionHelper parameterReflector)
 {
     return new NamedTypeDependencyResolverPolicy(
         parameterReflector.GetClosedParameterType(typeToBuild.GetGenericArguments()),
         name);
 }
 public static bool MethodHasOpenGenericParameters(MethodBase method)
 {
     Guard.ArgumentNotNull(method, "method");
     foreach (ParameterInfo param in method.GetParameters())
     {
         var r = new ReflectionHelper(param.ParameterType);
         if (r.IsOpenGeneric)
         {
             return true;
         }
     }
     return false;
 }
        /// <summary>
        /// Return a <see cref="IDependencyResolverPolicy"/> instance that will
        /// return this types value for the parameter.
        /// </summary>
        /// <param name="typeToBuild">Type that contains the member that needs this parameter. Used
        /// to resolve open generic parameters.</param>
        /// <returns>The <see cref="IDependencyResolverPolicy"/>.</returns>
        public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild)
        {
            GuardTypeToBuildIsGeneric(typeToBuild);
            GuardTypeToBuildHasMatchingGenericParameter(typeToBuild);
            Type typeToResolve = new ReflectionHelper(typeToBuild).GetNamedGenericParameter(genericParameterName);
            if (isArray)
            {
                typeToResolve = typeToResolve.MakeArrayType();
            }

            return DoGetResolverPolicy(typeToResolve, this.resolutionKey);
        }
Example #14
0
        private void CheckNonGeneric(Type parameterType)
        {
            if (parameterType.IsGenericParameter)
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ValueNotAllowedForGenericParameterType,
                        parameterType.Name,
                        this.Value));
            }

            var reflector = new ReflectionHelper(parameterType);

            if (reflector.IsOpenGeneric)
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ValueNotAllowedForOpenGenericType,
                        parameterType.Name,
                        this.Value));
            }

            if (reflector.IsGenericArray)
            {
                throw new InvalidOperationException(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ValueNotAllowedForGenericArrayType,
                        parameterType.Name,
                        this.Value));
            }
        }