Exemplo n.º 1
0
            internal override Expression Visit(Expression expression)
            {
                if (expression == null)
                {
                    return(expression);
                }
                Expression expression1;
                Expression expression2;

                if (this._replacementDictionary != null && this._replacementDictionary.TryGetValue(expression, out expression1))
                {
                    expression2 = expression1;
                }
                else
                {
                    bool             flag             = false;
                    LambdaExpression lambdaExpression = (LambdaExpression)null;
                    if (expression.NodeType == ExpressionType.Lambda && this._inlineDelegates != null)
                    {
                        lambdaExpression = (LambdaExpression)expression;
                        flag             = this._inlineDelegates.Contains(lambdaExpression);
                    }
                    if (flag)
                    {
                        Expression body = this.Visit(lambdaExpression.Body);
                        expression2 = (Expression)Expression.Constant(CodeGenEmitter.Compile(body.Type, body));
                    }
                    else
                    {
                        expression2 = base.Visit(expression);
                    }
                }
                return(expression2);
            }
            internal override Expression Visit(Expression expression)
            {
                if (null == expression)
                {
                    return(expression);
                }

                Expression result;

                // check to see if a substitution has been provided for this expression
                Expression replacement;

                if (null != _replacementDictionary &&
                    _replacementDictionary.TryGetValue(expression, out replacement))
                {
                    // once a substitution is found, we stop walking the sub-expression and
                    // return immediately (since recursive replacement is not needed or wanted)
                    result = replacement;
                }
                else
                {
                    // check if we need to precompile an inline delegate
                    var preCompile          = false;
                    LambdaExpression lambda = null;

                    if (expression.NodeType == ExpressionType.Lambda
                        &&
                        null != _inlineDelegates)
                    {
                        lambda     = (LambdaExpression)expression;
                        preCompile = _inlineDelegates.Contains(lambda);
                    }

                    if (preCompile)
                    {
                        // do replacement in the body of the lambda expression
                        var body = Visit(lambda.Body);

                        // compile to a delegate
                        result = Expression.Constant(CodeGenEmitter.Compile(body.Type, body));
                    }
                    else
                    {
                        result = base.Visit(expression);
                    }
                }

                return(result);
            }