// The constructor parameter in which the factory for creating decorated instances should be injected.
        private Expression BuildExpressionForScopedDecorateeFactoryDependencyParameter(
            ParameterInfo param, Type serviceType, Expression expr)
        {
            if (DecoratorHelpers.IsScopeDecorateeFactoryDependencyParameter(param.ParameterType, serviceType))
            {
                expr = CompilationHelpers.OptimizeScopedRegistrationsInObjectGraph(this.Container, expr);

                var instanceCreator =
                    Expression.Lambda(Expression.Convert(expr, serviceType)).Compile();

                var scopeParameter = Expression.Parameter(typeof(Scope), "scope");

                // Create an Func<Scope, [ServiceType>
                var scopedInstanceCreator = Expression.Lambda(
                    Expression.Call(
                        ResolveWithinThreadResolveScopeMethod.MakeGenericMethod(serviceType),
                        scopeParameter,
                        Expression.Constant(instanceCreator)),
                    scopeParameter)
                                            .Compile();

                return(Expression.Constant(scopedInstanceCreator));
            }

            return(null);
        }
        partial void TryCompileLambdaInDynamicAssembly(LambdaExpression expression, ref Delegate compiledDelegate)
        {
            compiledDelegate = null;

            if (this.container.Options.EnableDynamicAssemblyCompilation &&
                !CompilationHelpers.ExpressionNeedsAccessToInternals(expression))
            {
                compiledDelegate = CompileLambdaInDynamicAssemblyWithFallback(expression);
            }
        }
Ejemplo n.º 3
0
            private static Func <TService> CompileExpression(Expression expression)
            {
                try
                {
                    // Don't call BuildTransientDelegate, because that might do optimizations that are simply
                    // not needed, since the delegate will be called just once.
                    return(CompilationHelpers.CompileLambda <TService>(expression));
                }
                catch (Exception ex)
                {
                    string message = StringResources.ErrorWhileBuildingDelegateFromExpression(
                        typeof(TService), expression, ex);

                    throw new ActivationException(message, ex);
                }
            }
        // The constructor parameter in which the factory for creating decorated instances should be injected.
        private Expression BuildExpressionForDecorateeFactoryDependencyParameter(
            ParameterInfo parameter, Type serviceType, Expression expression)
        {
            if (IsDecorateeFactoryDependencyParameter(parameter, serviceType))
            {
                // We can't call CompilationHelpers.CompileExpression here, because it has a generic type and
                // we don't know the type at runtime here. We need to do some refactoring to CompilationHelpers
                // to get that working.
                expression = CompilationHelpers.OptimizeScopedRegistrationsInObjectGraph(this.Container, expression);

                var instanceCreator =
                    Expression.Lambda(Expression.Convert(expression, serviceType)).Compile();

                return(Expression.Constant(instanceCreator));
            }

            return(null);
        }
        private static Delegate CompileLambdaInDynamicAssemblyWithFallback(LambdaExpression expression)
        {
            try
            {
                var @delegate = CompilationHelpers.CompileLambdaInDynamicAssembly(expression,
                                                                                  "DynamicPropertyInjector" + GetNextInjectorClassId(),
                                                                                  "InjectProperties");

                // Test the creation. Since we're using a dynamically created assembly, we can't create every
                // delegate we can create using expression.Compile(), so we need to test this.
                CompilationHelpers.JitCompileDelegate(@delegate);

                return(@delegate);
            }
            catch
            {
                return(null);
            }
        }