/// <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); } }
/// <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; } }