何かのようなもの:
static class BooleanComplexifier
{
public static Expression<T> Process<T>(Expression<T> expression)
where T : class
{
var body = expression.Body;
if (body.Type == typeof(bool))
{
switch(body.NodeType)
{
case ExpressionType.Equal:
case ExpressionType.NotEqual:
case ExpressionType.GreaterThan:
case ExpressionType.GreaterThanOrEqual:
case ExpressionType.LessThan:
case ExpressionType.LessThanOrEqual:
return expression;
case ExpressionType.Not:
body = Expression.NotEqual(
((UnaryExpression)body).Operand,
Expression.Constant(true));
break;
default:
body = Expression.Equal(body,
Expression.Constant(true));
break;
}
return Expression.Lambda<T>(body, expression.Parameters);
}
return expression;
}
}
と:
Expression<Func<Foo, bool>> x = foo => foo.IsAlive,
y = foo => !foo.IsAlive;
var a = BooleanComplexifier.Process(x); // foo => foo.IsAlive == true
var b = BooleanComplexifier.Process(y); // foo => foo.IsAlive != true
//...
class Foo
{
public bool IsAlive { get;set; }
}
より複雑な処理では、次のExpressionVisitor
ものが必要になる場合があります。
class BooleanComplexifier : ExpressionVisitor
{
public static Expression<T> Process<T>(Expression<T> expression)
{
return (Expression<T>)new BooleanComplexifier().Visit(expression);
}
int bypass;
protected override Expression VisitBinary(BinaryExpression node)
{
if (bypass == 0 && node.Type == typeof(bool))
{
switch (node.NodeType)
{
case ExpressionType.And: // bitwise & - different to &&
case ExpressionType.Or: // bitwise | - different to ||
case ExpressionType.Equal:
case ExpressionType.NotEqual:
bypass++;
var result = base.VisitBinary(node);
bypass--;
return result;
}
}
return base.VisitBinary(node);
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (bypass == 0 && node.Type == typeof(bool))
{
switch(node.NodeType)
{
case ExpressionType.Not:
bypass++;
var result = Expression.NotEqual(
base.Visit(node.Operand),
Expression.Constant(true));
bypass--;
return result;
}
}
return base.VisitUnary(node);
}
protected override Expression VisitMember(MemberExpression node)
{
if(bypass == 0 && node.Type == typeof(bool))
{
return Expression.Equal(
base.VisitMember(node),
Expression.Constant(true));
}
return base.VisitMember(node);
}
}