/// <summary>
 /// Create call expression for delegate
 /// </summary>
 /// <param name="scope"></param>
 /// <param name="request"></param>
 /// <param name="activationConfiguration"></param>
 /// <param name="activationDelegate"></param>
 /// <returns></returns>
 protected virtual IActivationExpressionResult CreateCallExpression(IInjectionScope scope, IActivationExpressionRequest request, TypeActivationConfiguration activationConfiguration, ActivationStrategyDelegate activationDelegate)
 {
     return(ExpressionUtilities.CreateExpressionForDelegate(activationDelegate, activationConfiguration.ExternallyOwned,
                                                            scope, request));
 }
        private Expression CreateLocateExpression(ParameterInfo parameter, IInjectionScope scope, IActivationExpressionRequest request, TypeActivationConfiguration activationConfiguration)
        {
            var parameterInfo = FindParameterInfoExpression(parameter, activationConfiguration);

            if (parameterInfo?.ExportFunc is Delegate)
            {
                var newRequest = request.NewRequest(parameter.ParameterType, activationConfiguration.ActivationStrategy,
                                                    activationConfiguration.ActivationType, RequestType.ConstructorParameter, parameter, true);

                var delegateValue = (Delegate)parameterInfo.ExportFunc;

                var expressionCall = ExpressionUtilities.CreateExpressionForDelegate(delegateValue,
                                                                                     activationConfiguration.ExternallyOwned, scope, newRequest);

                var standardParameters = delegateValue.GetMethodInfo()
                                         .GetParameters()
                                         .All(p =>
                {
                    if (p.ParameterType == typeof(IExportLocatorScope))
                    {
                        return(true);
                    }

                    if (p.ParameterType == typeof(IDisposalScope))
                    {
                        return(true);
                    }

                    if (p.ParameterType == typeof(IInjectionContext))
                    {
                        return(true);
                    }

                    return(true);
                });

                if (standardParameters)
                {
                    return(expressionCall.Expression);
                }

                newRequest = request.NewRequest(parameter.ParameterType, activationConfiguration.ActivationStrategy,
                                                activationConfiguration.ActivationType, RequestType.ConstructorParameter, parameter, true);

                var compiledDelegate = request.Services.Compiler.CompileDelegate(scope, expressionCall);

                expressionCall =
                    ExpressionUtilities.CreateExpressionForDelegate(compiledDelegate, false, scope, newRequest);

                return(expressionCall.Expression);
            }

            var closedMethod = DynamicLocateMethodInfo.MakeGenericMethod(parameter.ParameterType);

            return(Expression.Call(Expression.Constant(this),
                                   closedMethod,
                                   request.Constants.ScopeParameter,
                                   request.Constants.RootDisposalScope,
                                   request.Constants.InjectionContextParameter,
                                   Expression.Constant(parameter.Name.ToLowerInvariant()),
                                   Expression.Constant(true),
                                   Expression.Constant(false),
                                   Expression.Default(parameter.ParameterType)));
        }