示例#1
0
        /// <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;
                ReflectionHelper       typeReflector   = new ReflectionHelper(method.First.DeclaringType);
                MethodReflectionHelper methodReflector = new MethodReflectionHelper(method.First);
                if (!methodReflector.MethodHasOpenGenericParameters && !typeReflector.IsOpenGeneric)
                {
                    selectedMethod = new SelectedMethod(method.First);
                }
                else
                {
                    Type[] closedMethodParameterTypes =
                        methodReflector.GetClosedParameterTypes(typeToBuild.GetTypeInfo().GenericTypeArguments);
                    selectedMethod = new SelectedMethod(
                        typeToBuild.GetMethodHierarchical(method.First.Name, closedMethodParameterTypes));
                }

                SpecifiedMemberSelectorHelper.AddParameterResolvers(
                    typeToBuild,
                    resolverPolicyDestination,
                    method.Second,
                    selectedMethod);
                yield return(selectedMethod);
            }
        }
        /// <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)
        {
            Unity.Utility.Guard.ArgumentNotNull(context, "context");

            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.GetTypeInfo().GenericTypeArguments);
                result = new SelectedConstructor(typeToBuild.GetConstructorInfo(closedCtorParameterTypes));
            }

            SpecifiedMemberSelectorHelper.AddParameterResolvers(typeToBuild, resolverPolicyDestination, parameterValues, result);
            return(result);
        }