/// <summary> /// Returns bindings for all assigned generic arguments in the specified list of bindings. /// </summary> public static void GetAssignedGenericArguments(BindingCollection bindings, Type concreteType, Type genericType) { Debug.Assert(bindings != null); Debug.Assert(concreteType != null); Debug.Assert(genericType != null); Debug.Assert(concreteType.Is(genericType)); IEnumerable <Tuple <Type, Type> > concreteTypes = GetConcreteTypes(concreteType, genericType); bindings.Expand(concreteTypes, (currentBindings, tuple) => { Type type = tuple.Item1; Type generic = tuple.Item2; Type[] assignedArguments = type.GetGenericArguments( ); Type[] unassignedArguments = generic.GetGenericArguments( ); Debug.Assert(assignedArguments.Length == unassignedArguments.Length); for (int i = 0; currentBindings.Count > 0 && i < unassignedArguments.Length; ++i) { Type unassignedArgument = unassignedArguments[i]; Type assignedArgument = assignedArguments[i]; if (assignedArgument.IsGenericParameter) { continue; } if (!unassignedArgument.IsGenericParameter) { if (unassignedArgument.ContainsGenericParameters) { GenericTypeResolver.GetAssignedGenericArguments(currentBindings, assignedArgument, unassignedArgument); } continue; } if (!assignedArgument.Is(unassignedArgument)) { currentBindings.Clear( ); break; } currentBindings.Reduce(b => { Binding existingBinding = Binding.ForArgument(b, unassignedArgument); return(existingBinding == null ? b.Add(new Binding(unassignedArgument, assignedArgument)) : (existingBinding.Type == assignedArgument ? b : null)); }); } if (genericType.IsGenericParameter) { currentBindings.Reduce(b => b.Add(new Binding(genericType, concreteType))); } }); }
private static IEnumerable <IInstanceCreator> GetMethodCreators(Type targetType, MethodBase method) { Debug.Assert(targetType != null); Debug.Assert(method != null); Debug.Assert(method.IsGenericMethod || !targetType.ContainsGenericParameters); // Ignore recursive parameters. var parameters = method.GetParameters( ); if (parameters.Any(p => p.ParameterType.Is(targetType) || p.ParameterType.Is(method.DeclaringType))) { yield break; } // Retrieve creators for each parameter. var availableArguments = parameters.Select((p) => TypeCreator.GetInstanceCreators(p.ParameterType).AsEnumerable( )); // Call constructor with all argument permutations. foreach (var arguments in Permuter.Permute(availableArguments)) { // If method is concrete, use it. if (!method.IsGenericMethod) { yield return(WeakInstanceCreator.ForMethod(targetType, method, arguments)); } // Otherwise, try to resolve generic arguments on method. else if (method is MethodInfo) { var methodInfo = (MethodInfo)method; var bindings = new BindingCollection(Binding.EmptyBindings); for (int i = 0; i < parameters.Length; ++i) { ParameterInfo parameter = parameters[i]; IInstanceCreator argument = arguments[i]; if (parameter.ParameterType.ContainsGenericParameters) { GenericTypeResolver.GetAssignedGenericArguments(bindings, argument.InstanceType, parameter.ParameterType); } } foreach (LinkList <Binding> b in bindings) { var concreteMethod = GenericTypeResolver.MakeConcreteMethod(methodInfo, b); if (concreteMethod != null) { targetType = concreteMethod.ReturnType; yield return(WeakInstanceCreator.ForMethod(targetType, concreteMethod, arguments)); } } } } }
private static IEnumerable <IInstanceCreator> GetConstructorCreators(Type targetType, Type availableType) { Debug.Assert(targetType != null); Debug.Assert(availableType != null); Debug.Assert(!availableType.IsAbstract); if (targetType.ContainsGenericParameters) { Type definition = targetType.IsGenericParameter ? targetType : targetType.GetGenericTypeDefinition( ); var bindings = new BindingCollection(Binding.EmptyBindings); GenericTypeResolver.GetAssignedGenericArguments(bindings, availableType, definition); foreach (var binding in bindings) { Type resolvedType = definition.IsGenericParameter ? Binding.ForArgument(binding, definition).Type : GenericTypeResolver.MakeConcreteType(definition, binding); targetType = resolvedType; break; } } var creators = new List <IInstanceCreator>( ); // If type is an enumeration, get creator for enum values. if (availableType.IsEnum || availableType == typeof(bool)) { creators.AddRange(WeakInstanceCreator.ForEnum(availableType)); } // Otherwise, get creator for any default constructor. else if (Constraint.HasDefaultConstructor(availableType)) { creators.Add(WeakInstanceCreator.ForType(availableType)); } // Get creators for any parameterized constructors. var parameterizedConstructors = availableType.GetConstructors( ).Where((c) => c.GetParameters( ).Length > 0).ToArray( ); if (parameterizedConstructors.Length < 3) { foreach (var constructor in parameterizedConstructors) { creators.AddRange(TypeCreator.GetMethodCreators(targetType, constructor)); } } return(creators); }
private static ICollection <MethodInfo> GetConcreteMethods(MethodInfo method, LinkList <Binding> bindings) { Debug.Assert(method != null); Debug.Assert(bindings != null); // Try to resolve generic arguments on method. var openArguments = GenericTypeResolver.GetOpenGenericArguments(method.GetGenericArguments( )); var concreteBindings = new BindingCollection(bindings); GenericTypeResolver.BindGenericArguments(concreteBindings, openArguments); var concreteMethods = concreteBindings.Transform((b) => MakeConcreteMethod(method, openArguments, b)); // If generic arguments cannot be resolved statically, try to bind method arguments by searching for creatable parameter types. if (concreteMethods.Count == 0) { var genericParameterTypes = method.GetParameters( ) .Select(p => p.ParameterType) .Where(t => t.IsGenericType) .ToArray( ); Debug.Assert(genericParameterTypes.All(t => !t.IsGenericParameter)); var creatableParameterTypes = genericParameterTypes .Select(t => TypeCreator.GetCreators(t) .Select(c => c.InstanceType) .Distinct( ) ); var parameterBindings = new BindingCollection(bindings); parameterBindings.Expand(Permuter.Permute(creatableParameterTypes), (b, permutation) => { for (int i = 0; i < permutation.Length; ++i) { Type permutationType = permutation[i]; Type genericParameterType = genericParameterTypes[i]; GenericTypeResolver.GetAssignedGenericArguments(b, permutationType, genericParameterType); } }); concreteMethods = parameterBindings.Transform((b) => MakeConcreteMethod(method, openArguments, b)); } return(concreteMethods); }
protected override IEnumerable <LinkList <Binding> > SatisfyCore(Type availableType, LinkList <Binding> bindings) { var newBindings = new List <LinkList <Binding> >( ); var currentBindngs = new BindingCollection(bindings); // Check if the available type derives from the base type. if (availableType.Is(this.requiredBaseType_)) { // Add any generic arguments assigned by the base type. if (this.requiredBaseType_.IsGenericType && !this.requiredBaseType_.ContainsGenericParameters) { IEnumerable <Type> correspondingTypes = GenericTypeResolver.GetCorrespondingBaseTypes(availableType, this.requiredBaseType_); foreach (Type correspondingType in correspondingTypes) { if (correspondingType.ContainsGenericParameters) { GenericTypeResolver.GetAssignedGenericArguments(currentBindngs, this.requiredBaseType_, correspondingType); } } } // Check all concrete version of the available type. foreach (LinkList <Binding> b in currentBindngs) { var concreteTypes = GenericTypeResolver.GetConcreteTypes(availableType, b); foreach (Type concreteAvailableType in concreteTypes) { // Add any newly assigned bindings from concrete and base types. var assignedBindings = new BindingCollection(b); GenericTypeResolver.GetAssignedGenericArguments(assignedBindings, concreteAvailableType, availableType); if (this.requiredBaseType_.ContainsGenericParameters) { GenericTypeResolver.GetAssignedGenericArguments(assignedBindings, concreteAvailableType, this.requiredBaseType_); } newBindings.AddRange(assignedBindings); } } } return(newBindings); }