Example #1
0
        /// <summary>
        /// Simplifies an expression tree by evaluating any branches of the tree that do not include
        /// lambda parameter references.  This will remove references to variables external to the lambda
        /// by converting them to constants, perform arithmetic, and execute method calls as needed.
        /// For example, a call to "str.ToUpper()" where string is an external variable would be simplified
        /// to a <see cref="ConstantExpression"/> containing the uppercase version of str.
        /// </summary>
        /// <param name="expression">Expression to simplify.</param>
        /// <returns>The simplified expression.</returns>
        public static Expression Simplify(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var visitor = new LambdaSimplifyingExpressionVisitor();

            expression = visitor.Visit(expression);

            if (visitor._isEvaluatable)
            {
                // The entire tree can simplified

                return(ConvertToConstant(expression));
            }
            else
            {
                return(expression);
            }
        }
Example #2
0
        protected override Expression VisitLambda <T>(Expression <T> node)
        {
            if (_parameter != null)
            {
                throw new NotSupportedException("Only one lambda expression is allowed in a subdocument path.");
            }

            _parameter = node.Parameters[0];

            // Simplify the body before processing.  This makes any external variable references constants, and also
            // reduces any expressions that can be simplified.
            var body = LambdaSimplifyingExpressionVisitor.Simplify(node.Body);

            body = Visit(body);

            if (!_parameterEncountered)
            {
                throw new NotSupportedException("The first statement in a subdocument path must be a reference to the lambda parameter.");
            }

            return(node.Update(body, node.Parameters));
        }
        /// <summary>
        /// Simplifies an expression tree by evaluating any branches of the tree that do not include
        /// lambda parameter references.  This will remove references to variables external to the lambda
        /// by converting them to constants, perform arithmetic, and execute method calls as needed.
        /// For example, a call to "str.ToUpper()" where string is an external variable would be simplified
        /// to a <see cref="ConstantExpression"/> containing the uppercase version of str.
        /// </summary>
        /// <param name="expression">Expression to simplify.</param>
        /// <returns>The simplified expression.</returns>
        public static Expression Simplify(Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            var visitor = new LambdaSimplifyingExpressionVisitor();

            expression = visitor.Visit(expression);

            if (visitor._isEvaluatable)
            {
                // The entire tree can simplified

                return ConvertToConstant(expression);
            }
            else
            {
                return expression;
            }
        }