ExpressionTree 重写 - 导航属性的 MakeMemberAccess()
与上一个问题模糊相关< /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为将 C# 代码翻译成英语,然后将其翻译成表达式创建代码可能会有所帮助:
表达式
x.NavigationProperty.Name
实际上意味着“访问属性NavigationProperty 在
x
上,然后访问结果上的属性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 propertyNavigationProperty
onx
and then access propertyName
on the result. Now, the code: