ExpressionTree 重写 - 导航属性的 MakeMemberAccess()

发布于 2024-12-03 12:19:31 字数 1630 浏览 0 评论 0原文

上一个问题模糊相关< /em>

注意:我正在使用 ExpressionTree 访问者的派生,如所解释的 此处

在我的 < code>VisitMemberAccess 方法 我当前使用以下内容创建 MemberExpressions:

// `mapping` is a class used to map EntityA's members to EntityB's members
return Expression.MakeMemberAccess(Visit(m.Expression), mapping.TargetMemberInfo);

在大多数情况下,这是有效的。

给定一些测试类...

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

代码将正确映射 (EntityA x) => x.Id(EntityB x) => x.MyId 非常棒,而且工作起来很可爱。当您引入导航属性时,我的问题就出现了:

public class EntityB
{
    public long MyId {get; set; }
    public EntityBDetails NavigationProperty { get; set; }
}    

public class EntityBDetails
{
    public string MyName { get; set; }
}

鉴于上述微不足道的情况,我想要 (EntityA x) x =>; x.Name 映射到 (EntityB x) x => x.NavigationProperty.Name。这就是问题所在,我不知道要向 MakeMemberAccess 提供什么来完成这项工作...我可以比较 mapping.TargetMemberInfo.DeclaringType == mapping.TargetMemberInfo.ReflectedType确定是否涉及导航属性,但如何创建必要的MemberExpression?

提前致谢!

注意:我正在开发的代码库是 VB; C# 往往会在 SO 上获得更好/更快的答案,因此我手动进行了转换。如果我犯了愚蠢的拼写错误/等等,请告诉我

Related vaguely to a previous question

Note : I'm using a derivative of the ExpressionTree visitor as explained here

In my VisitMemberAccess method I currently create MemberExpressions using something along the lines of:

// `mapping` is a class used to map EntityA's members to EntityB's members
return Expression.MakeMemberAccess(Visit(m.Expression), mapping.TargetMemberInfo);

For the most part, this works.

Given some test classes...

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

The code will correctly map (EntityA x) => x.Id to (EntityB x) => x.MyId which is great and works lovely. My problem comes when you introduce navigation properties:

public class EntityB
{
    public long MyId {get; set; }
    public EntityBDetails NavigationProperty { get; set; }
}    

public class EntityBDetails
{
    public string MyName { get; set; }
}

Given the above trivial case, I would want (EntityA x) x => x.Name to map to (EntityB x) x => x.NavigationProperty.Name. And therelies the problem, I have no idea what to supply to MakeMemberAccess to make this work... I can compare mapping.TargetMemberInfo.DeclaringType == mapping.TargetMemberInfo.ReflectedType to determine whether there is a navigation property involved, but how do I create the necessary MemberExpression?

Thanks in advance!

NB: The code base I'm working on is VB; C# tends to get better/faster answers on SO so I've converted by hand. Let me know if I've made silly typo's/etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2024-12-10 12:19:31

我认为将 C# 代码翻译成英语,然后将其翻译成表达式创建代码可能会有所帮助:

表达式 x.NavigationProperty.Name 实际上意味着“访问属性 NavigationProperty 在 x 上,然后访问结果上的属性 Name。现在,代码:

ParameterExpression x = …;
var navigationProperty = typeof(EntityB).GetProperty("NavigationProperty");
var name = typeof(EntityBDetails).GetProperty("Name");

var navigationPropertyAccess = Expression.MakeMemberAccess(x, navigationProperty);
var nameAccess = Expression.MakeMemberAccess(navigationPropertyAccess , name);

I think it could help to translate the C# code into English, and then translate that into an expression-creating code:

The expression x.NavigationProperty.Name actually means “access property NavigationProperty on x and then access property Name on the result. Now, the code:

ParameterExpression x = …;
var navigationProperty = typeof(EntityB).GetProperty("NavigationProperty");
var name = typeof(EntityBDetails).GetProperty("Name");

var navigationPropertyAccess = Expression.MakeMemberAccess(x, navigationProperty);
var nameAccess = Expression.MakeMemberAccess(navigationPropertyAccess , name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文