表达式树 - 替代表达式

时间:2017-07-05 10:59:29

标签: c# expression-trees

考虑两个表达式树:

Expression<Func<float, float, float>> f1 = (x, y) => x + y;
Expression<Func<float, float>> f2 = x => x * x;

我想将表达式f2替换为f1的第二个参数,并获取以下表达式:

Expression<Func<float, float, float>> f3 = (x, y) => x + y * y;

最简单的方法是使用Expression.LambdaExpression.Invoke,但结果将如下所示

(x, y) => f1(x, f2(y))

但由于无法正确处理invoke / lambda的ORM限制,这对我来说是不可接受的。

是否有可能在没有完全遍历表达式树的情况下构造表达式?可以找到满足我需求的工作示例here,但我想要更简单的解决方案。

1 个答案:

答案 0 :(得分:3)

如果没有对两个表达式进行完全遍历,则无法执行此操作。幸运的是,ExpressionVisitor使得遍历完全非常简单:

class ReplaceParameter : ExpressionVisitor {
    private readonly Expression replacement;
    private readonly ParameterExpression parameter;
    public ReplaceParameter(
        ParameterExpression parameter
    ,   Expression replacement
    ) {
        this.replacement = replacement;
        this.parameter = parameter;
    }
    protected override Expression VisitParameter(ParameterExpression node) {
        return node == parameter ? replacement : node;
    }
}

使用此访问者两次完成替换:

Expression<Func<float,float,float>> f1 = (x, y) => x + y;
Expression<Func<float,float>> f2 = x => x * x;
var pX = f2.Parameters[0];
var pY = f1.Parameters[1];
var replacerF2 = new ReplaceParameter(pX, pY);
var replacerF1 = new ReplaceParameter(pY, replacerF2.Visit(f2.Body));
var modifiedF1 = Expression.Lambda(
    replacerF1.Visit(f1.Body)
,   f1.Parameters
);
Console.WriteLine(modifiedF1);

以上打印

(x, y) => (x + (y * y))

Demo.