/// <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> /// 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); } }
public void CanGetIfAnyParametersAreOpenGeneric() { Type openType = typeof(Pathological <,>); ConstructorInfo ctor = openType.GetTypeInfo().DeclaredConstructors.ElementAt(0); MethodReflectionHelper helper = new MethodReflectionHelper(ctor); Assert.True(helper.MethodHasOpenGenericParameters); }
public void CanGetIfAnyParametersAreOpenGeneric() { Type openType = typeof(Pathological <,>); ConstructorInfo ctor = openType.GetConstructors()[0]; MethodReflectionHelper helper = new MethodReflectionHelper(ctor); Assert.IsTrue(helper.MethodHasOpenGenericParameters); }
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[, ])); }
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>)); }
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); }
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); }
/// <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; }