替换表达式树中的参数值

本文关键字:参数 表达式 替换 | 更新日期: 2023-09-27 18:08:12

在搜索了很长一段时间后,我仍然没有找到我想要的答案。我找到了关于在树中添加和删除参数的答案,但没有找到任何关于替换特定参数的答案。

我的第一个方法正在按我希望的方式工作,我需要将partitionKey值替换为Uri转义值,然后返回未转义的结果。

public override IList<T> GetRowEntityList(string partitionKey)
{
    IList<T> rowEntities =  base.GetRowEntityList(Uri.EscapeDataString(partitionKey));
    return rowEntities.Select(UnEscapeRowEntity).ToList();
}

我遇到的问题是重写此方法以实现相同的行为。我已经知道类型T具有属性PartitionKeyRowKey,但也可以具有任何其他数量的属性。

对于示例谓词:

x => x.RowKey == "foo/bar" && x.SomeValue == "test" 

我希望它能成为

x => x.RowKey == Uri.EscapeDataString("foo/bar") && x.SomeValue == "test"  

有办法做到这一点吗?

我的基类使用这个谓词使用Where(predicate)调用对包含T类型实体的表进行表查找

public override IList<T> GetRowEntityList(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
    //modify predicate value here
    return base.GetRowEntityList(predicate);
}

替换表达式树中的参数值

您需要实现ExpressionVisitor:

class MyVisitor : ExpressionVisitor
{
    protected override Expression VisitBinary(BinaryExpression node)
    {
        if(CheckForMatch(node.Left))
            return Expression.Equal(node.Left, Rewrite(node.Right));
        if(CheckForMatch(node.Right))
            return Expression.Equal(Rewrite(node.Left), node.Right);
        return Expression.MakeBinary(node.NodeType, Visit(node.Left), Visit(node.Right));
    }
    private bool CheckForMatch(Expression e)
    {
        MemberExpression me = e as MemberExpression;
        if(me == null)
            return false;
        if(me.Member.Name == "RowKey" || me.Member.Name == "PartitionKey")
            return true;
        else
            return false;
    }
    private Expression Rewrite(Expression e)
    {
        MethodInfo mi = typeof(Uri).GetMethod("EscapeDataString");
        return Expression.Call(mi, e);
    }
}

我认为这是对的。测试起来有点困难。请注意,这只适用于(x => x.RowKey == "some string")的有限情况。它不适用于(x => x.RowKey.Equals("somestring")。它也不适用于(x => x.RowKey() == "some string")

然后使用实现的访问者重写谓词:

Expression<Func<T, bool>> predicate = (s => s.RowKey == "1 2");
ExpressionVisitor v = new MyVisitor();
Expression<Func<T, bool>> rewrittenPredicate = v.Visit(predicate);
//rewrittenPredicate then tests if s.RowKey == "1%202"