コード例 #1
0
        /// <summary>
        /// Compiles an <see cref="Expression"/> to a <see cref="Func{T,R}"/> of <see cref="IResolutionScope"/>, <see cref="Delegate"/>.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="resolutionContext">The resolution context.</param>
        /// <param name="containerConfiguration">The container configuration.</param>
        /// <returns>The compiled delegate.</returns>
        public static Func <IResolutionScope, Delegate> CompileDynamicDelegate(this Expression expression,
                                                                               ResolutionContext resolutionContext, ContainerConfiguration containerConfiguration)
        {
            if (!resolutionContext.DefinedVariables.IsEmpty)
            {
                resolutionContext.SingleInstructions.Add(expression);
                expression = resolutionContext.SingleInstructions.AsBlock(resolutionContext.DefinedVariables.Walk());
            }

            if (containerConfiguration.ExternalExpressionCompiler != null)
            {
                return((Func <IResolutionScope, Delegate>)containerConfiguration.ExternalExpressionCompiler(
                           expression.AsLambda(resolutionContext.CurrentScopeParameter)));
            }

#if IL_EMIT
            if (!expression.TryEmit(out var factory, typeof(Func <IResolutionScope, Delegate>), typeof(Delegate),
                                    resolutionContext.CurrentScopeParameter))
            {
                factory = expression.AsLambda <Func <IResolutionScope, Delegate> >(resolutionContext.CurrentScopeParameter).Compile();
            }

            return((Func <IResolutionScope, Delegate>)factory);
#else
            return(expression.AsLambda <Func <IResolutionScope, Delegate> >(resolutionContext.CurrentScopeParameter).Compile());
#endif
        }
コード例 #2
0
        /// <summary>
        /// Compiles an <see cref="Expression"/> to a <see cref="Func{T,R}"/> of <see cref="IResolutionScope"/>, <see cref="object"/>.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <param name="resolutionContext">The resolution context.</param>
        /// <param name="containerConfiguration">The container configuration.</param>
        /// <returns>The compiled delegate.</returns>
        public static Func <IResolutionScope, object> CompileDelegate(this Expression expression,
                                                                      ResolutionContext resolutionContext, ContainerConfiguration containerConfiguration)
        {
            if (expression.NodeType == ExpressionType.Constant)
            {
                var instance = ((ConstantExpression)expression).Value;
                return(scope => instance);
            }

            if (!resolutionContext.DefinedVariables.IsEmpty)
            {
                resolutionContext.SingleInstructions.Add(expression);
                expression = resolutionContext.SingleInstructions.AsBlock(resolutionContext.DefinedVariables.Walk());
            }

            if (containerConfiguration.ExternalExpressionCompiler != null)
            {
                return((Func <IResolutionScope, object>)containerConfiguration.ExternalExpressionCompiler(
                           expression.AsLambda(resolutionContext.CurrentScopeParameter)));
            }

#if IL_EMIT
            if (!expression.TryEmit(out var factory, typeof(Func <IResolutionScope, object>), typeof(object),
                                    resolutionContext.CurrentScopeParameter))
            {
                factory = expression.AsLambda(resolutionContext.CurrentScopeParameter).Compile();
            }

            return((Func <IResolutionScope, object>)factory);
#else
            return(expression.AsLambda <Func <IResolutionScope, object> >(resolutionContext.CurrentScopeParameter).Compile());
#endif
        }