Пример #1
0
        public static Expression <Hoisted <TIn, TOut> > Hoist(Expression <Func <TIn, TOut> > expr)
        {
            HoistingExpressionVisitor <TIn, TOut> hoistingExpressionVisitor = new HoistingExpressionVisitor <TIn, TOut>();
            Expression body = hoistingExpressionVisitor.Visit(expr.Body);

            return(Expression.Lambda <Hoisted <TIn, TOut> >(body, new ParameterExpression[]
            {
                expr.Parameters[0],
                HoistingExpressionVisitor <TIn, TOut> ._hoistedConstantsParamExpr
            }));
        }
Пример #2
0
        public static Expression <Hoisted <TIn, TOut> > Hoist(Expression <Func <TIn, TOut> > expr)
        {
            // rewrite Expression<Func<TIn, TOut>> as Expression<Hoisted<TIn, TOut>>

            var visitor             = new HoistingExpressionVisitor <TIn, TOut>();
            var rewrittenBodyExpr   = visitor.Visit(expr.Body);
            var rewrittenLambdaExpr = Expression.Lambda <Hoisted <TIn, TOut> >(
                rewrittenBodyExpr,
                expr.Parameters[0],
                _hoistedConstantsParamExpr
                );

            return(rewrittenLambdaExpr);
        }
Пример #3
0
            private static Func <TIn, TOut> CompileFromFingerprint(Expression <Func <TIn, TOut> > expr)
            {
                List <object> capturedConstants;
                ExpressionFingerprintChain fingerprintChain = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);

                if (fingerprintChain != null)
                {
                    Hoisted <TIn, TOut> del = CachedExpressionCompiler.Compiler <TIn, TOut> ._fingerprintedCache.GetOrAdd(fingerprintChain, delegate(ExpressionFingerprintChain _)
                    {
                        Expression <Hoisted <TIn, TOut> > expression = HoistingExpressionVisitor <TIn, TOut> .Hoist(expr);
                        return(expression.Compile());
                    });

                    return((TIn model) => del(model, capturedConstants));
                }
                return(null);
            }
Пример #4
0
            private static Func <TIn, TOut> CompileFromFingerprint(Expression <Func <TIn, TOut> > expr)
            {
                List <object> capturedConstants;
                ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);

                if (fingerprint != null)
                {
                    var del = _fingerprintedCache.GetOrAdd(fingerprint, _ => {
                        // Fingerprinting succeeded, but there was a cache miss. Rewrite the expression
                        // and add the rewritten expression to the cache.

                        var hoistedExpr = HoistingExpressionVisitor <TIn, TOut> .Hoist(expr);
                        return(hoistedExpr.Compile());
                    });
                    return(model => del(model, capturedConstants));
                }

                // couldn't be fingerprinted
                return(null);
            }