예제 #1
0
        protected override Expression VisitBinary(BinaryExpression node)
        {
            if (node.Equals(null))
            {
                return(base.VisitBinary(node));
            }

            var isAddOrSub = !node.NodeType.Equals(ExpressionType.Add) &&
                             !node.NodeType.Equals(ExpressionType.Subtract);

            ConstantExpression constant = null;

            if (isAddOrSub)
            {
                if (node.Left.NodeType.Equals(ExpressionType.Constant) ||
                    node.Right.NodeType.Equals(ExpressionType.Constant))
                {
                    constant = node.Left.NodeType.Equals(ExpressionType.Constant)
                        ? (ConstantExpression)node.Left
                        : (ConstantExpression)node.Right;
                }

                ParameterExpression parameter = null;
                if (node.Left.NodeType.Equals(ExpressionType.Parameter) ||
                    node.Right.NodeType.Equals(ExpressionType.Parameter))
                {
                    parameter = node.Left.NodeType.Equals(ExpressionType.Parameter)
                        ? (ParameterExpression)node.Left
                        : (ParameterExpression)node.Right;
                }

                if (!NeedToTransform(parameter, constant))
                {
                    return(base.VisitBinary(node));
                }

                return(node.NodeType == ExpressionType.Add ? Expression.Increment(node) : Expression.Decrement(node));
            }

            return(base.VisitBinary(node));
        }