예제 #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);
            }
        }
예제 #2
0
 /// <summary>
 /// Return the sequence of methods to call while building the target object.
 /// </summary>
 /// <param name="context">Current build context.</param>
 /// <returns>Sequence of methods to call.</returns>
 public IEnumerable <SelectedMethod> SelectMethods(IBuilderContext context)
 {
     foreach (Pair <MethodInfo, IEnumerable <InjectionParameterValue> > method in methods)
     {
         Type                   typeToBuild = BuildKey.GetType(context.BuildKey);
         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.GetGenericArguments());
             selectedMethod = new SelectedMethod(
                 typeToBuild.GetMethod(method.First.Name, closedMethodParameterTypes));
         }
         SpecifiedMemberSelectorHelper.AddParameterResolvers(
             typeToBuild,
             context.PersistentPolicies,
             method.Second,
             selectedMethod);
         yield return(selectedMethod);
     }
 }
예제 #3
0
        public void CanGetIfAnyParametersAreOpenGeneric()
        {
            Type            openType = typeof(Pathological <,>);
            ConstructorInfo ctor     = openType.GetTypeInfo().DeclaredConstructors.ElementAt(0);

            MethodReflectionHelper helper = new MethodReflectionHelper(ctor);

            Assert.True(helper.MethodHasOpenGenericParameters);
        }
예제 #4
0
        public void CanGetIfAnyParametersAreOpenGeneric()
        {
            Type            openType = typeof(Pathological <,>);
            ConstructorInfo ctor     = openType.GetConstructors()[0];

            MethodReflectionHelper helper = new MethodReflectionHelper(ctor);

            Assert.IsTrue(helper.MethodHasOpenGenericParameters);
        }
예제 #5
0
        public void CanGetClosedParameterArrayTypesFromOpenOnes()
        {
            Type                   openType   = typeof(TypeWithArrayConstructorParameters <,>);
            Type                   closedType = typeof(TypeWithArrayConstructorParameters <Account, User>);
            ConstructorInfo        ctor       = openType.GetTypeInfo().DeclaredConstructors.ElementAt(0);
            MethodReflectionHelper helper     = new MethodReflectionHelper(ctor);

            Type[] closedTypes = helper.GetClosedParameterTypes(closedType.GenericTypeArguments);

            closedTypes.AssertContainsExactly(typeof(User[]), typeof(Account[, ]));
        }
예제 #6
0
        public void CanGetClosedParameterTypesFromOpenOnes()
        {
            Type                   openType   = typeof(Pathological <,>);
            Type                   closedType = typeof(Pathological <Account, User>);
            ConstructorInfo        ctor       = openType.GetTypeInfo().DeclaredConstructors.ElementAt(0);
            MethodReflectionHelper helper     = new MethodReflectionHelper(ctor);

            Type[] closedTypes = helper.GetClosedParameterTypes(closedType.GenericTypeArguments);

            closedTypes.AssertContainsExactly(typeof(ICommand <User>), typeof(ICommand <Account>));
        }
예제 #7
0
        public void CanGetClosedParameterArrayTypesFromOpenOnes()
        {
            Type                   openType   = typeof(TypeWithArrayConstructorParameters <,>);
            Type                   closedType = typeof(TypeWithArrayConstructorParameters <Account, User>);
            ConstructorInfo        ctor       = openType.GetConstructors()[0];
            MethodReflectionHelper helper     = new MethodReflectionHelper(ctor);

            Type[] closedTypes = helper.GetClosedParameterTypes(closedType.GetGenericArguments());

            CollectionAssert.AreEqual(
                Sequence.Collect(typeof(User[]), typeof(Account[, ])),
                closedTypes);
        }
예제 #8
0
        public void CanGetClosedParameterTypesFromOpenOnes()
        {
            Type                   openType   = typeof(Pathological <,>);
            Type                   closedType = typeof(Pathological <Account, User>);
            ConstructorInfo        ctor       = openType.GetConstructors()[0];
            MethodReflectionHelper helper     = new MethodReflectionHelper(ctor);

            Type[] closedTypes = helper.GetClosedParameterTypes(closedType.GetGenericArguments());

            CollectionAssert.AreEqual(
                Sequence.Collect(typeof(ICommand <User>), typeof(ICommand <Account>)),
                closedTypes);
        }
예제 #9
0
 /// <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;
     ctorReflector        = new MethodReflectionHelper(ctor);
     this.parameterValues = parameterValues;
 }