相关隐约到$p$pvious问题 的
注意:我使用的是防爆pressionTree游客的衍生物作为解释的这里 的
Note : I'm using a derivative of the ExpressionTree visitor as explained here
在我的 VisitMemberAccess
方法,我现在创建使用的线沿线的东西MemberEx pressions:
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; }
}
在code将正确映射(EntityA X)=> x.Id
到(EntityB X)=> x.MyId
这是伟大的,工作可爱。当你引入导航属性我的问题就来了:
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; }
}
鉴于上述简单的情况,我想(EntityA x)x => x.Name
映射到(EntityB x)x => x.NavigationProperty.Name
。而therelies的问题,我不知道该怎么提供给 MakeMemberAccess
来使这项工作......我可以比较 mapping.TargetMemberInfo.DeclaringType == mapping.TargetMemberInfo.ReflectedType
来确定是否有涉及导航属性,但我如何创建必要的MemberEx pression?
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?
在此先感谢!
注:在code碱基我的工作是VB; C#中往往获得更好/更快速的答案在SO所以我一直用手工转换。让我知道如果我做了愚蠢的拼写错误的/ etc 的
我觉得它可以帮助翻译C#code成英文,然后它转换成一个前pression创造code
I think it could help to translate the C# code into English, and then translate that into an expression-creating code:
这位前pression x.NavigationProperty.Name
其实就是访问属性 NavigationProperty
在 X
,然后访问属性名称
的结果。现在,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);
上一篇:C#定制观察的集合 - 我应该使用成分还是继承?成分
下一篇:Spring Boot Security - Postman 给出 401 UnauthorizedSecurity、Boot、Spring、Unauthorized