本文整理汇总了C#中System.Linq.Expressions.BinaryExpression.IsLogicalOperation方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryExpression.IsLogicalOperation方法的具体用法?C# BinaryExpression.IsLogicalOperation怎么用?C# BinaryExpression.IsLogicalOperation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Linq.Expressions.BinaryExpression
的用法示例。
在下文中一共展示了BinaryExpression.IsLogicalOperation方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitBinary
protected override Expression VisitBinary(BinaryExpression node)
{
if (node.IsLogicalOperation())
{
var newLeft = Visit(node.Left);
var newRight = Visit(node.Right);
var constantLeft = newLeft as ConstantExpression;
var constantRight = newRight as ConstantExpression;
if (node.NodeType == ExpressionType.AndAlso)
{
if ((constantLeft != null)
&& (constantLeft.Type == typeof(bool)))
{
// true && a => a
// false && a => false
return (bool)constantLeft.Value ? newRight : newLeft;
}
if ((constantRight != null)
&& (constantRight.Type == typeof(bool)))
{
// a && true => a
// a && false => false
return (bool)constantRight.Value ? newLeft : newRight;
}
}
if (node.NodeType == ExpressionType.OrElse)
{
if ((constantLeft != null)
&& (constantLeft.Type == typeof(bool)))
{
// true || a => true
// false || a => a
return (bool)constantLeft.Value ? newLeft : newRight;
}
if ((constantRight != null)
&& (constantRight.Type == typeof(bool)))
{
// a || true => true
// a || false => a
return (bool)constantRight.Value ? newRight : newLeft;
}
}
}
return base.VisitBinary(node);
}
示例2: VisitBinary
protected override Expression VisitBinary(BinaryExpression node)
{
if (!_insideConditionalTest
&& (node.IsComparisonOperation()
|| node.IsLogicalOperation()))
{
return Expression.Condition(
node,
Expression.Constant(true, typeof(bool)),
Expression.Constant(false, typeof(bool)));
}
return base.VisitBinary(node);
}
示例3: VisitBinary
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
protected override Expression VisitBinary(BinaryExpression node)
{
if (node.IsLogicalOperation())
{
var newLeft = Visit(node.Left);
var newRight = Visit(node.Right);
var constantLeft = newLeft as ConstantExpression;
var constantRight = newRight as ConstantExpression;
if (node.NodeType == ExpressionType.AndAlso)
{
if ((constantLeft != null)
&& (constantLeft.Type == typeof(bool)))
{
// true && a => a
// false && a => false
return (bool)constantLeft.Value ? newRight : newLeft;
}
if ((constantRight != null)
&& (constantRight.Type == typeof(bool)))
{
// a && true => a
// a && false => false
return (bool)constantRight.Value ? newLeft : newRight;
}
}
if (node.NodeType == ExpressionType.OrElse)
{
if ((constantLeft != null)
&& (constantLeft.Type == typeof(bool)))
{
// true || a => true
// false || a => a
return (bool)constantLeft.Value ? newLeft : newRight;
}
if ((constantRight != null)
&& (constantRight.Type == typeof(bool)))
{
// a || true => true
// a || false => a
return (bool)constantRight.Value ? newRight : newLeft;
}
}
}
// a == true -> a
if (node.NodeType == ExpressionType.Equal
&& node.Left.Type.UnwrapNullableType() == typeof(bool)
&& node.Right.Type.UnwrapNullableType() == typeof(bool))
{
var newLeft = Visit(node.Left);
var newRight = Visit(node.Right);
var leftConstant = newLeft as ConstantExpression;
if (leftConstant != null && (bool?)leftConstant.Value == true)
{
return newRight.Type == typeof(bool) ? newRight : Expression.Convert(newRight, typeof(bool));
}
var rightConstant = newRight as ConstantExpression;
if (rightConstant != null && (bool?)rightConstant.Value == true)
{
return newLeft.Type == typeof(bool) ? newLeft : Expression.Convert(newLeft, typeof(bool));
}
}
return base.VisitBinary(node);
}