我想不出更好的方法来表达这个问题,但我想做的是通过在评估 LambdaExpression 之前处理 LambdaExpression 的实例Expression<Func<MyObject, FilterObject, bool>>
来减少 LambdaExpression 的签名。Expression<Func<MyObject, bool>>
FilterObject
这是一个简单的例子:
AddFilter("Filter Name", FilterTypes.All,
(x, y) => GetConjunctionResult(
x.PersonA.IsSomething, x.PersonB.IsSomething, y.ConjunctionType));
private static bool GetConjunctionResult(bool personA, bool personB,
ConjunctionType conjunctionType)
{
switch (conjunctionType)
{
case ConjunctionType.Both:
return personA && personB:
case ConjunctionType.Either:
return personA && personB;
case ConjunctionType.PersonA:
return personA;
case ConjunctionType.PersonB:
return personB;
case ConjunctionType.Neither:
return !personA && !personB;
}
}
所以我希望这个重载AddFilter
来创建一个类型的对象FilterObject
并将其嵌入到 LambdaExpression 中,如下所示:
var filter = new FilterObject();
// create Expression<Func<MyObject, bool>> lambda = x => GetConjunctionResult(
// x.PersonA.IsSomething, x.PersonB.IsSomething, filter.ConjunctionType));
现在可能有更好的方法来做到这一点,所以我愿意接受任何完全避开这种方法的建议。