/// <summary>
        /// Create enumerable expression using a custom creator
        /// </summary>
        /// <param name="scope">injection scope</param>
        /// <param name="request">expression request</param>
        /// <param name="arrayExpressionCreator">array creator</param>
        /// <param name="enumerableCreator">custom enumerable creator</param>
        /// <returns></returns>
        protected virtual IActivationExpressionResult CreateEnumerableExpressionUsingCustomCreator(IInjectionScope scope,
                                                                                                   IActivationExpressionRequest request, IArrayExpressionCreator arrayExpressionCreator,
                                                                                                   IEnumerableCreator enumerableCreator)
        {
            var enumerableType = request.ActivationType.GenericTypeArguments[0];

            var arrayType = enumerableType.MakeArrayType();

            var newRequest =
                request.NewRequest(arrayType, request.RequestingStrategy,
                                   request.RequestingStrategy?.ActivationType, request.RequestType, request.Info, true, true);

            newRequest.SetFilter(request.Filter);
            newRequest.SetEnumerableComparer(request.EnumerableComparer);

            var arrayExpression = arrayExpressionCreator.GetArrayExpression(scope, newRequest);

            var enumerableExpression =
                Expression.Call(Expression.Constant(enumerableCreator),
                                CreateEnumerableMethod.MakeGenericMethod(enumerableType),
                                request.ScopeParameter,
                                arrayExpression.Expression);

            var returnResult = request.Services.Compiler.CreateNewResult(request, enumerableExpression);

            returnResult.AddExpressionResult(returnResult);

            return(returnResult);
        }
        /// <summary>
        /// Create enumerable expression that is an array
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="request"></param>
        /// <param name="arrayExpressionCreator"></param>
        /// <returns></returns>
        protected virtual IActivationExpressionResult CreateEnumerableExpressionUsingArrayExpression(IInjectionScope scope,
                                                                                                     IActivationExpressionRequest request, IArrayExpressionCreator arrayExpressionCreator)
        {
            var enumerableType = request.ActivationType.GenericTypeArguments[0];

            var arrayType = enumerableType.MakeArrayType();

            var newRequest = request.NewRequest(arrayType, request.RequestingStrategy,
                                                request.RequestingStrategy?.ActivationType, request.RequestType, request.Info, true, true);

            newRequest.SetFilter(request.Filter);
            newRequest.SetEnumerableComparer(request.EnumerableComparer);
            newRequest.SetLocateKey(request.LocateKey);

            var arrayExpression = arrayExpressionCreator.GetArrayExpression(scope, newRequest);

            return(arrayExpression);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get a linq expression to satisfy the request
        /// </summary>
        /// <param name="scope">scope</param>
        /// <param name="request">request</param>
        /// <returns></returns>
        public virtual IActivationExpressionResult GetActivationExpression(IInjectionScope scope, IActivationExpressionRequest request)
        {
            var activationExpressionResult = GetValueFromRequest(scope, request, request.ActivationType, null);

            if (activationExpressionResult != null)
            {
                return(activationExpressionResult);
            }

            activationExpressionResult = GetValueFromInjectionValueProviders(scope, request);

            if (activationExpressionResult != null)
            {
                return(activationExpressionResult);
            }

            activationExpressionResult = GetActivationExpressionFromStrategies(scope, request);

            if (activationExpressionResult != null)
            {
                return(activationExpressionResult);
            }

            if (request.ActivationType.IsArray)
            {
                return(ArrayExpressionCreator.GetArrayExpression(scope, request));
            }

            if (request.ActivationType.IsConstructedGenericType &&
                request.ActivationType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
            {
                return(EnumerableExpressionCreator.GetEnumerableExpression(scope, request, ArrayExpressionCreator));
            }

            var wrapperResult = WrapperExpressionCreator.GetActivationExpression(scope, request);

            if (wrapperResult != null)
            {
                return(wrapperResult);
            }

            if (scope.MissingExportStrategyProviders.Any())
            {
                lock (scope.GetLockObject(InjectionScope.ActivationStrategyAddLockName))
                {
                    activationExpressionResult = GetActivationExpressionFromStrategies(scope, request);

                    if (activationExpressionResult != null)
                    {
                        return(activationExpressionResult);
                    }

                    wrapperResult = WrapperExpressionCreator.GetActivationExpression(scope, request);

                    if (wrapperResult != null)
                    {
                        return(wrapperResult);
                    }

                    request.Services.Compiler.ProcessMissingStrategyProviders(scope, request);

                    activationExpressionResult = GetActivationExpressionFromStrategies(scope, request);

                    if (activationExpressionResult != null)
                    {
                        return(activationExpressionResult);
                    }

                    wrapperResult = WrapperExpressionCreator.GetActivationExpression(scope, request);

                    if (wrapperResult != null)
                    {
                        return(wrapperResult);
                    }
                }
            }

            var parent = scope.Parent as IInjectionScope;

            if (parent != null)
            {
                return(GetActivationExpression(parent, request));
            }

            return(GetValueFromInjectionContext(scope, request));
        }