/// <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; } }
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.GenericTypeArguments); } return new OptionalDependencyResolverPolicy(typeToResolve, this.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()); }
public override IDependencyResolverPolicy GetResolverPolicy(Type typeToBuild) { Guard.ArgumentNotNull(typeToBuild, "typeToBuild"); var parameterReflector = new ReflectionHelper(ParameterType); if (parameterReflector.IsGenericArray) { return this.CreateGenericArrayResolverPolicy(typeToBuild, parameterReflector); } if (parameterReflector.IsOpenGeneric || parameterReflector.Type.IsGenericParameter) { return this.CreateGenericResolverPolicy(typeToBuild, parameterReflector); } return this.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)); } }
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)); }
/// <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); }
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; }
private IDependencyResolverPolicy CreateGenericResolverPolicy(Type typeToBuild, ReflectionHelper parameterReflector) { return new NamedTypeDependencyResolverPolicy( parameterReflector.GetClosedParameterType(typeToBuild.GenericTypeArguments), this.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); if (this.isArray) { typeToResolve = typeToResolve.MakeArrayType(); } return this.DoGetResolverPolicy(typeToResolve, this.resolutionKey); }