private static IEnumerable <IInstanceCreator> GetFactoryCreators(Type targetType, Type availableType)
        {
            Debug.Assert(targetType != null);
            Debug.Assert(availableType != null);
            Debug.Assert(availableType.IsAbstract && availableType.IsSealed);

            var factoryMethods =
                from m in availableType.GetMethods(BindingFlags.Public | BindingFlags.Static)
                // Check that method is a "GetInstances" factory method, and returns an IEnumerable<> collection.
                where m.Name.StartsWith("Get", StringComparison.OrdinalIgnoreCase) &&
                m.Name.EndsWith("Instances", StringComparison.OrdinalIgnoreCase) &&
                m.ReturnType.IsGenericType &&
                m.ReturnType.GetGenericTypeDefinition( ) == typeof(IEnumerable <>)
                // Check that the enumerable collection is of type Func<>.
                let enumerableArgType = m.ReturnType.GetGenericArguments( )[0]
                                        where enumerableArgType.IsGenericType &&
                                        enumerableArgType.GetGenericTypeDefinition( ) == typeof(Func <>)
                                        // Check that the return type of the Func<> delegate is valid for the target type.
                                        let funcArgType = enumerableArgType.GetGenericArguments( )[0]
                                                          where targetType.Is(funcArgType)
                                                          select m;

            // Call each factory method, returning creators from the resulting Func<> delegates.
            var factoryCreators =
                from method in factoryMethods
                from methodCreator in TypeCreator.GetMethodCreators(method.ReturnType, method)
                let funcCollection = (System.Collections.IEnumerable)methodCreator.CreateInstance( )
                                     from func in funcCollection.Cast <object>( )
                                     let creator = WeakInstanceCreator.ForDelegate(func)
                                                   select creator;

            return(factoryCreators);
        }
            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);
            }