我不知道这是否是最好的方法,但你可以这样做:
public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
Expression<Func<TContainer,TMember>> getMemberExpression,
Expression<Func<TMember,bool>> memberPredicateExpression)
{
ParameterExpression x = Expression.Parameter(typeof(TContainer), "x");
return Expression.Lambda<Func<TContainer, bool>>(
Expression.Invoke(
memberPredicateExpression,
Expression.Invoke(
getMemberExpression,
x)),
x);
}
用法:
var expr = CreatePredicate(
(Foo f) => f.Bar,
bar => bar % 2 == 0);
结果:
x => Invoke(bar => ((bar % 2) == 0), Invoke(f => f.Bar, x))
我想得到类似的东西会更好x => x.Bar % 2 == 0
,但它可能会更难......
编辑:实际上,表达访问者并不难:
public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>(
Expression<Func<TContainer,TMember>> getMemberExpression,
Expression<Func<TMember,bool>> memberPredicateExpression)
{
return CombineExpressionVisitor.Combine(
getMemberExpression,
memberPredicateExpression);
}
class CombineExpressionVisitor : ExpressionVisitor
{
private readonly ParameterExpression _parameterToReplace;
private readonly Expression _replacementExpression;
private CombineExpressionVisitor(ParameterExpression parameterToReplace, Expression replacementExpression)
{
_parameterToReplace = parameterToReplace;
_replacementExpression = replacementExpression;
}
public static Expression<Func<TSource, TResult>> Combine<TSource, TMember, TResult>(
Expression<Func<TSource, TMember>> memberSelector,
Expression<Func<TMember, TResult>> resultSelector)
{
var visitor = new CombineExpressionVisitor(
resultSelector.Parameters[0],
memberSelector.Body);
return Expression.Lambda<Func<TSource, TResult>>(
visitor.Visit(resultSelector.Body),
memberSelector.Parameters);
}
protected override Expression VisitParameter(ParameterExpression parameter)
{
if (parameter == _parameterToReplace)
return _replacementExpression;
return base.VisitParameter(parameter);
}
}
它给出以下表达式:
f => ((f.Bar % 2) == 0)