本文整理匯總了C#中System.Linq.Expressions.Expression.Evaluate方法的典型用法代碼示例。如果您正苦於以下問題:C# Expression.Evaluate方法的具體用法?C# Expression.Evaluate怎麽用?C# Expression.Evaluate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Linq.Expressions.Expression
的用法示例。
在下文中一共展示了Expression.Evaluate方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: VisitExpression
protected override Expression VisitExpression(Expression exp)
{
if (exp != null &&
_canBeSimplified[exp] &&
!(exp.NodeType == ExpressionType.Lambda) &&
!(exp.NodeType == ExpressionType.Quote))
{
var eval = exp.Evaluate();
return Expression.Constant(eval, exp.Type);
}
return base.VisitExpression(exp);
}
示例2: AnalyzeConstant
protected virtual Expression AnalyzeConstant(Expression expression, BuilderContext builderContext)
{
// we try to find a non-constant operand, and if we do, we won't change this expression
foreach (var operand in expression.GetOperands())
{
if (!(operand is ConstantExpression))
return expression;
}
// now, we just simply return a constant with new value
try
{
var optimizedExpression = Expression.Constant(expression.Evaluate());
// sometimes, optimizing an expression changes its type, and we just can't allow this.
if (optimizedExpression.Type == expression.Type)
return optimizedExpression;
}
// if we fail to evaluate the expression, then just return it
catch (ArgumentException) { }
return expression;
}
示例3: AnalyzeBinaryBoolean
private Expression AnalyzeBinaryBoolean(Expression expression, BuilderContext builderContext)
{
if (expression.Type != typeof(bool))
return expression;
var bin = expression as BinaryExpression;
if (bin == null)
return expression;
bool canOptimizeLeft = bin.Left.NodeType == ExpressionType.Constant && bin.Left.Type == typeof(bool);
bool canOptimizeRight = bin.Right.NodeType == ExpressionType.Constant && bin.Right.Type == typeof(bool);
if (canOptimizeLeft && canOptimizeRight)
return Expression.Constant(expression.Evaluate());
if (canOptimizeLeft || canOptimizeRight)
switch (expression.NodeType)
{
case ExpressionType.AndAlso:
if (canOptimizeLeft)
if ((bool)bin.Left.Evaluate())
return bin.Right; // (TRUE and X) == X
else
return bin.Left; // (FALSE and X) == FALSE
if (canOptimizeRight)
if ((bool)bin.Right.Evaluate())
return bin.Left; // (X and TRUE) == X
else
return bin.Right; // (X and FALSE) == FALSE
break;
case ExpressionType.OrElse:
if (canOptimizeLeft)
if ((bool)bin.Left.Evaluate())
return bin.Left; // (TRUE or X) == TRUE
else
return bin.Right; // (FALSE or X) == X
if (canOptimizeRight)
if ((bool)bin.Right.Evaluate())
return bin.Right; // (X or TRUE) == TRUE
else
return bin.Left; // (X or FALSE) == X
break;
case ExpressionType.Equal:
// TODO: this optimization should work for Unary Expression Too
// this actually produce errors becouse of string based Sql generation
canOptimizeLeft = canOptimizeLeft && bin.Right is BinaryExpression;
if (canOptimizeLeft)
if ((bool)bin.Left.Evaluate())
return bin.Right; // (TRUE == X) == X
else
return Expression.Not(bin.Right); // (FALSE == X) == not X
canOptimizeRight = canOptimizeRight && bin.Left is BinaryExpression;
// TODO: this optimization should work for Unary Expression Too
// this actually produce errors becouse of string based Sql generation
if (canOptimizeRight)
if ((bool)bin.Right.Evaluate())
return bin.Left; // (X == TRUE) == X
else
return Expression.Not(bin.Left); // (X == FALSE) == not X
break;
case ExpressionType.NotEqual:
canOptimizeLeft = canOptimizeLeft && bin.Right is BinaryExpression;
// TODO: this optimization should work for Unary Expression Too
// this actually produce errors becouse of string based Sql generation
if (canOptimizeLeft)
if ((bool)bin.Left.Evaluate())
return Expression.Not(bin.Right); // (TRUE != X) == not X
else
return bin.Right; // (FALSE != X) == X
canOptimizeRight = canOptimizeRight && bin.Left is BinaryExpression;
// TODO: this optimization should work for Unary Expression Too
// this actually produce errors becouse of string based Sql generation
if (canOptimizeRight)
if ((bool)bin.Right.Evaluate())
return Expression.Not(bin.Left); // (X != TRUE) == not X
else
return bin.Left; // (X != FALSE) == X
break;
}
return expression;
}
示例4: AnalyzeConstant
protected virtual Expression AnalyzeConstant(Expression expression, BuilderContext builderContext)
{
// we try to find a non-constant operand, and if we do, we won't change this expression
foreach (var operand in expression.GetOperands())
{
if (!(operand is ConstantExpression))
return expression;
}
if (expression.NodeType == ExpressionType.Parameter)
return expression;
if (expression.NodeType == (ExpressionType)SpecialExpressionType.Like)
return expression;
// SETuse
// If the value of the first SpecialExpressionType change this 999 should change too
if ((short)expression.NodeType > 999)
return expression;
// now, we just simply return a constant with new value
try
{
var optimizedExpression = Expression.Constant(expression.Evaluate());
// sometimes, optimizing an expression changes its type, and we just can't allow this.
if (optimizedExpression.Type == expression.Type)
return optimizedExpression;
}
// if we fail to evaluate the expression, then just return it
catch (ArgumentException)
{
return expression;
}
return expression;
}