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); }
public static IInstanceCreator ForInstanceCreator(Type targetType, IInstanceCreator creator) { Debug.Assert(creator != null); Debug.Assert(targetType != null); Debug.Assert(targetType.IsGenericType && targetType.GetGenericTypeDefinition( ) == typeof(IInstanceCreator <>)); IInstanceCreator finalCreator = creator; Type creatorType = creator.GetType( ); Type targetCreatedType = targetType.GetGenericArguments( )[0]; // Ensure target type is closed. Type finalTargetType = targetType; if (finalTargetType.ContainsGenericParameters) { targetCreatedType = GenericTypeResolver.GetCorrespondingBaseTypes(finalCreator.InstanceType, targetCreatedType).Single( ); finalTargetType = typeof(IInstanceCreator <>).MakeGenericType(targetCreatedType); } // If the creator does not have the same type as the target, wrap the creator to return instances of the target base type. if (!creatorType.Is(finalTargetType)) { Type actualCreatedType = creatorType.GetInterfaces( ) .Single(i => i.IsGenericType && i.GetGenericTypeDefinition( ) == typeof(IInstanceCreator <>)) .GetGenericArguments( )[0]; Type wrapperType = typeof(InstanceCreatorWrapper <,>).MakeGenericType(targetCreatedType, actualCreatedType); finalCreator = (IInstanceCreator)Activator.CreateInstance(wrapperType, creator); } // Get IInstanceCreator to return the given creator instance. Type funcType = typeof(Func <>).MakeGenericType(finalTargetType); var lambda = Expression.Lambda(funcType, Expression.Constant(finalCreator, finalTargetType)); var nestingCreator = WeakInstanceCreator.ForDelegate(lambda.Compile( )); return(nestingCreator); }