重写ExpressionTree -参数'x'不在范围内

本文关键字:范围内 ExpressionTree 重写 参数 | 更新日期: 2023-09-27 18:07:55

如果我在以下代码中犯了任何错误/输入错误,请不要生气,只需在这里添加注释,我会立即修复-谢谢

目标

Expression<TDelegate>从一个EntityA重新映射到一个EntityB

我怀疑这种事情以前已经做过了,但我还没有发现任何特别有用的链接,所以请随时为我指出正确的方向。

到目前为止,我所拥有的是一个类的选择,它们组合在一起,允许在两个给定类的实体成员之间创建映射。例如,表面API可能具有以下签名:
public void AddMemberBinding<TEntityA, TEntityB, TMember>(Func<TEntityA, TMember> entityAMemberSelector, Func<TEntityB, TMember> entityBMemberSelector)
{
    // does some magic, eventually storing the necessary MemberInfo details required to
    // "remap" MemberExpressions (MemberAccess) from TEntityA to TEntityB
}

给定下列类…

public class EntityA
{
    public long Id { get; set; }
    public string Name { get; set ;}
}
public class EntityB
{
    public long MyId { get; set; }
    public string MyName { get; set; }
}

您将能够创建绑定的东西沿着一行…

public static void AddBindings()
{
    AddMemberBinding((EntityA n) => n.Id, (EntityB n) => n.MyId);
    AddMemberBinding((EntityA n) => n.Name, (EntityB n) => n.MyName);
}

在我的情况下,我有Assembly1知道EntityA是什么,但不知道EntityB。我有Assembly2,它知道EntityAEntityB是什么,并且对Assembly1可见。Assembly2Assembly1提供了一个方法,可能如下所示:

public static IEnumerable<EntityA> GetData<TResult>(Expression<Func<EntityA, bool>> criteria, Expression<Func<EntityA, TResult>> selector)
{
    // EntityB's are stored in a database, I could do one of two things here...
    // 1) Return all EntitieB's and then apply criteria and selector through the IEnumerable extensions
    //    this would be sub-optimal - particularly if there are millions of EntityB's!
    // 2) "Transmute" (for lack of a better word) the expressions provided, using the keymappings
    //    specified earlier, to derive expressions that can be passed through to the QueryableProvider
    // ... as you might have guessed, I opted for #2
}

我使用的是ExpressionTree Visitor的派生版本,带有以下重写的方法:

protected override Expression VisitLambda(LambdaExpression lambda)
{
    Type targetParameterType = lambda.Parameters[0].Type;
    Type targetExpressionType = lambda.Type;
    If (lambda.Parameters.Count = 1 && lambda.Parameters(0).Type == EntityA)
    {
        targetParameterType = EntityB;
        // the `GetResultType` method called gets the TResult type from Func<T, TResult>
        Type targetExpressionResultType = GetResultType(lambda);
        targetExpressionType = gettype(Func<EntityB, targetExpressionResultType>) 
    }
    // this is probably wrong, but maintains the current (last) parameter instance
    // I started doing this after reading about a similar issue to mine found:
    // https://stackoverflow.com/questions/411738/expression-or-the-parameter-item-is-not-in-scope
    this.CurrentLambdaParameters = lambda.Parameters.Select(x => Expression.Parameter(targetParameterType, x.Name));
    Expression body = this.Visit(lambda.Body);
    If (body != lambda.Body)
    {
        return Expression.Lambda(targetExpressionType, body, this.CurrentLambdaParameters);
    }
    return lambda;
}
protected override Expression VisitMemberAccess(MemberExpression m)
{
    // at this point I go off and look at key mappings, fetch the mapping required, etc
    // the entity I retrieve has a `TargetMemberInfo` property which is a `MemberInfo` for the
    // member on the target entity - speaks for itself I guess...
    return Expression.MakeMemberAccess(this.CurrentParameters.Single(), myMappingClassThing.TargetMemberInfo);
}

问题

说了这么多,做了这么多,当我用测试用例运行代码时,我在标题中得到了错误…我可以从描述中看到这是一个参数问题,但是我读过一个类似的问题,我希望我通过使用我在修改根lambda表达式时创建的参数来解决VisitMemberAccess方法中的问题- ParameterExpression的相同实例修复了我认为的链接问题?

似乎我没有很好地理解这部分流程。问题是,我哪里做错了!?我需要用这些参数表达式做什么才能使它们在范围内?

提前感谢你的回答,如果你读到这里,向你致敬!!

重写ExpressionTree -参数'x'不在范围内

在查看Jon的非常相似的问题并进行重构以合并他的几个实践时,我无意中发现了答案。我注意到VisitParameter从未被调用,其原因是我对VisitMemberAccess的重写终止了通过表达式树的递归。

它应该看起来像(使用不同的重载):

protected override Expression VisitMemberAccess(MemberExpression m) 
{ 
    return Expression.MakeMemberAccess(Visit(m.Expression), myMappingClassThing.TargetMemberInfo); 
} 

与确保您不会创建相同参数的多个实例以及所有内容很好地放在一起相结合。

再次感谢Jon!div =)