【问题标题】:ExpressionTree rewrite - The parameter 'x' is not in scopeExpressionTree 重写 - 参数“x”不在范围内
【发布时间】:2011-09-07 10:39: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);
}

问题

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

我似乎不太了解该过程的这一部分。问题是,我哪里做错了!?我需要对这些 ParameterExpressions 做什么才能使其“在范围内”?

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

【问题讨论】:

  • 这让我想起了过去的similar question。看看那个对你有没有帮助。注意:最终结果要复杂得多,要自动考虑几种情况,但核心思想是相同的。
  • 这看起来很完美,乔恩。我会尽快告诉你我的情况。我只是希望你的问题在我开始我的搜索时出现在搜索中!!
  • @Jon - 结果对我来说答案要简单得多。我的重写方法 VisitMemberAccess 没有继续递归,而是将 MemberAccess 视为叶子。 =/由于代码省略,在我的问题中可能并不明显。不过,您的问题/答案非常有用,重构以使用您的一些更好的做法让我直接找到它。谢谢!

标签: c# .net-3.5 lambda expression-trees


【解决方案1】:

在查看 Jon's remarkably similiar question 并重构以合并他的一些实践时,我更喜欢我自己的实现,但我偶然发现了答案。我注意到从未调用过VisitParameter,原因是我对VisitMemberAccess 的覆盖停止了通过表达式树的递归。

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

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

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

再次感谢乔恩! =)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多