Пример #1
0
        bool TryRewriteMemberExpressionNode(MemberExpression node, out Expression newNode)
        {
            newNode = null;
            if (this.locationReferences != null)
            {
                foreach (LocationReference locationReference in this.locationReferences)
                {
                    if (node.Member.Name == locationReference.Name)
                    {
                        if (locationReference is ILocationReferenceWrapper)
                        {
                            newNode = ExpressionUtilities.CreateIdentifierExpression(((ILocationReferenceWrapper)locationReference).LocationReference);
                        }
                        else
                        {
                            newNode = ExpressionUtilities.CreateIdentifierExpression(locationReference);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
        private Expression Rewrite(Expression expression, ReadOnlyCollection <ParameterExpression> lambdaParameters)
        {
            Func <Expression, Expression>       selector = null;
            Func <ElementInit, ElementInit>     func2    = null;
            Func <MemberBinding, MemberBinding> func3    = null;
            Func <Expression, Expression>       func4    = null;
            Func <Expression, Expression>       func5    = null;
            Func <Expression, Expression>       func6    = null;
            Func <Expression, Expression>       func7    = null;
            Func <Expression, Expression>       func8    = null;

            if (expression == null)
            {
                return(null);
            }
            switch (expression.NodeType)
            {
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
            case ExpressionType.And:
            case ExpressionType.AndAlso:
            case ExpressionType.Coalesce:
            case ExpressionType.Divide:
            case ExpressionType.Equal:
            case ExpressionType.ExclusiveOr:
            case ExpressionType.GreaterThan:
            case ExpressionType.GreaterThanOrEqual:
            case ExpressionType.LeftShift:
            case ExpressionType.LessThan:
            case ExpressionType.LessThanOrEqual:
            case ExpressionType.Modulo:
            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
            case ExpressionType.NotEqual:
            case ExpressionType.Or:
            case ExpressionType.OrElse:
            case ExpressionType.Power:
            case ExpressionType.RightShift:
            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
            {
                BinaryExpression expression2 = (BinaryExpression)expression;
                return(Expression.MakeBinary(expression2.NodeType, this.Rewrite(expression2.Left, lambdaParameters), this.Rewrite(expression2.Right, lambdaParameters), expression2.IsLiftedToNull, expression2.Method, (LambdaExpression)this.Rewrite(expression2.Conversion, lambdaParameters)));
            }

            case ExpressionType.ArrayLength:
            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
            case ExpressionType.Negate:
            case ExpressionType.NegateChecked:
            case ExpressionType.Not:
            case ExpressionType.Quote:
            case ExpressionType.TypeAs:
            {
                UnaryExpression expression17 = (UnaryExpression)expression;
                return(Expression.MakeUnary(expression17.NodeType, this.Rewrite(expression17.Operand, lambdaParameters), expression17.Type, expression17.Method));
            }

            case ExpressionType.ArrayIndex:
            {
                MethodCallExpression expression10 = expression as MethodCallExpression;
                if (expression10 == null)
                {
                    BinaryExpression expression11 = (BinaryExpression)expression;
                    return(Expression.ArrayIndex(this.Rewrite(expression11.Left, lambdaParameters), this.Rewrite(expression11.Right, lambdaParameters)));
                }
                if (func4 == null)
                {
                    func4 = a => this.Rewrite(a, lambdaParameters);
                }
                return(Expression.ArrayIndex(this.Rewrite(expression10.Object, lambdaParameters), expression10.Arguments.Select <Expression, Expression>(func4)));
            }

            case ExpressionType.Call:
            {
                MethodCallExpression expression12 = (MethodCallExpression)expression;
                if (func5 == null)
                {
                    func5 = a => this.Rewrite(a, lambdaParameters);
                }
                return(Expression.Call(this.Rewrite(expression12.Object, lambdaParameters), expression12.Method, expression12.Arguments.Select <Expression, Expression>(func5)));
            }

            case ExpressionType.Conditional:
            {
                ConditionalExpression expression3 = (ConditionalExpression)expression;
                return(Expression.Condition(this.Rewrite(expression3.Test, lambdaParameters), this.Rewrite(expression3.IfTrue, lambdaParameters), this.Rewrite(expression3.IfFalse, lambdaParameters)));
            }

            case ExpressionType.Constant:
                return(expression);

            case ExpressionType.Invoke:
            {
                InvocationExpression expression4 = (InvocationExpression)expression;
                if (selector == null)
                {
                    selector = a => this.Rewrite(a, lambdaParameters);
                }
                return(Expression.Invoke(this.Rewrite(expression4.Expression, lambdaParameters), expression4.Arguments.Select <Expression, Expression>(selector)));
            }

            case ExpressionType.Lambda:
            {
                LambdaExpression expression5 = (LambdaExpression)expression;
                return(Expression.Lambda(expression5.Type, this.Rewrite(expression5.Body, expression5.Parameters), expression5.Parameters));
            }

            case ExpressionType.ListInit:
            {
                ListInitExpression expression6 = (ListInitExpression)expression;
                if (func2 == null)
                {
                    func2 = ei => Expression.ElementInit(ei.AddMethod, (IEnumerable <Expression>)(from arg in ei.Arguments select this.Rewrite(arg, lambdaParameters)));
                }
                return(Expression.ListInit((NewExpression)this.Rewrite(expression6.NewExpression, lambdaParameters), expression6.Initializers.Select <ElementInit, ElementInit>(func2)));
            }

            case ExpressionType.MemberAccess:
            {
                MemberExpression expression8 = (MemberExpression)expression;
                return(Expression.MakeMemberAccess(this.Rewrite(expression8.Expression, lambdaParameters), expression8.Member));
            }

            case ExpressionType.MemberInit:
            {
                MemberInitExpression expression9 = (MemberInitExpression)expression;
                if (func3 == null)
                {
                    func3 = b => this.Rewrite(b, lambdaParameters);
                }
                return(Expression.MemberInit((NewExpression)this.Rewrite(expression9.NewExpression, lambdaParameters), expression9.Bindings.Select <MemberBinding, MemberBinding>(func3)));
            }

            case ExpressionType.UnaryPlus:
            {
                UnaryExpression expression18 = (UnaryExpression)expression;
                return(Expression.UnaryPlus(this.Rewrite(expression18.Operand, lambdaParameters), expression18.Method));
            }

            case ExpressionType.New:
            {
                NewExpression expression15 = (NewExpression)expression;
                if (expression15.Constructor != null)
                {
                    if (func8 == null)
                    {
                        func8 = a => this.Rewrite(a, lambdaParameters);
                    }
                    return(Expression.New(expression15.Constructor, expression15.Arguments.Select <Expression, Expression>(func8)));
                }
                return(expression);
            }

            case ExpressionType.NewArrayInit:
            {
                NewArrayExpression expression13 = (NewArrayExpression)expression;
                if (func6 == null)
                {
                    func6 = e => this.Rewrite(e, lambdaParameters);
                }
                return(Expression.NewArrayInit(expression13.Type.GetElementType(), expression13.Expressions.Select <Expression, Expression>(func6)));
            }

            case ExpressionType.NewArrayBounds:
            {
                NewArrayExpression expression14 = (NewArrayExpression)expression;
                if (func7 == null)
                {
                    func7 = e => this.Rewrite(e, lambdaParameters);
                }
                return(Expression.NewArrayBounds(expression14.Type.GetElementType(), expression14.Expressions.Select <Expression, Expression>(func7)));
            }

            case ExpressionType.Parameter:
            {
                ParameterExpression expression7 = (ParameterExpression)expression;
                if ((lambdaParameters == null) || !lambdaParameters.Contains(expression7))
                {
                    string name = expression7.Name;
                    for (LocationReferenceEnvironment environment = this.environment; environment != null; environment = environment.Parent)
                    {
                        foreach (LocationReference reference in environment.GetLocationReferences())
                        {
                            if (string.Equals(reference.Name, name, StringComparison.OrdinalIgnoreCase))
                            {
                                LocationReference reference3;
                                LocationReference locationReference = reference;
                                if (this.metadata.HasValue && this.metadata.Value.TryGetInlinedLocationReference(reference, out reference3))
                                {
                                    locationReference = reference3;
                                }
                                return(ExpressionUtilities.CreateIdentifierExpression(locationReference));
                            }
                        }
                    }
                    return(expression7);
                }
                return(expression7);
            }

            case ExpressionType.TypeIs:
            {
                TypeBinaryExpression expression16 = (TypeBinaryExpression)expression;
                return(Expression.TypeIs(this.Rewrite(expression16.Expression, lambdaParameters), expression16.TypeOperand));
            }

            case ExpressionType.Assign:
            {
                BinaryExpression expression22 = (BinaryExpression)expression;
                return(Expression.Assign(this.Rewrite(expression22.Left, lambdaParameters), this.Rewrite(expression22.Right, lambdaParameters)));
            }

            case ExpressionType.Block:
            {
                BlockExpression            expression19 = (BlockExpression)expression;
                List <ParameterExpression> list         = new List <ParameterExpression>();
                foreach (ParameterExpression expression20 in expression19.Variables)
                {
                    list.Add((ParameterExpression)this.Rewrite(expression20, lambdaParameters));
                }
                List <Expression> list2 = new List <Expression>();
                foreach (Expression expression21 in expression19.Expressions)
                {
                    list2.Add(this.Rewrite(expression21, lambdaParameters));
                }
                return(Expression.Block((IEnumerable <ParameterExpression>)list, (IEnumerable <Expression>)list2));
            }
            }
            return(expression);
        }