gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:43 26 4
gpt4 key购买 nike

如果我在下面的代码中犯了任何错误/输入错误,请不要生气,只需在这里发表评论,我会立即修复 - 谢谢

目标

重新映射 Expression<TDelegate>来自一个EntityAEntityB .

我怀疑以前有人做过这种事情,但我没有找到任何特别有用的链接,所以请随时为我指出正确的方向。

到目前为止,我所拥有的是一系列类,这些类组合起来允许在两个给定类的实体成员之间创建映射。例如,表面 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
}

给定以下类(class)...

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

您将能够使用类似于...的方式创建绑定(bind)

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 可见. Assembly2提供了一种方法 Assembly1可能如下所示:

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我曾希望我已经解决了 VisitMemberAccess 中的问题。使用我在修改根 lambda 表达式时创建的参数的方法 - ParameterExpression 的同一实例修复了我认为的链接问题问题?

我好像不太了解那部分流程。问题是,我哪里错了!?我需要对这些 ParameterExpressions 做什么才能使它们“在范围内”?

预先感谢您的回答,如果您读到这里,恭喜您!

最佳答案

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

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

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

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

再次感谢乔恩! =)

关于c# - ExpressionTree 重写 - 参数 'x' 不在范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7332488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com