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

时间:2011-09-07 10:39:56

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

如果我在下面的代码中犯了任何错误/错误,请不要浪费,只需在此处发表评论,我马上就会解决 - 谢谢

目标

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);
}

问题

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

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

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

1 个答案:

答案 0 :(得分:1)

在查看Jon's remarkably similiar question并重构他的几个实践时,我更倾向于自己的实现,我偶然发现了答案。我注意到VisitParameter从未被调用过,其原因是我对VisitMemberAccess的覆盖停止了表达式树的递归。

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

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

将此与确保您不创建相同参数的多个实例相结合,并且所有内容都很好地插在一起。

再次感谢Jon! =)