How can I Create a Expression.Property of a child object

巧了我就是萌 提交于 2019-12-10 13:18:19

问题


normally I create an expresion in this way.

ParameterExpression pe = Expression.Parameter(typeof(object1), "x");

string Name = "property1";

MemberExpression left = Expression.Property(pe, (object1).GetProperty(Name));

it produces left = x => x.property1

I need to know how can I produce

left = x => x.Object2.property1

if Name = "Object2.property1"; and object2 is a child to object1

Thanks in advance


回答1:


I don't quite understand what you want. Is it a property chain (say: x.Prop1.Prop2)?

var pe = Expression.Parameter(typeof(object1));
var property1 = typeof(object1).GetProperty(Name1);
var property2 = property1.PropertyType.GetProperty(Name2);
var inner = Expression.Property(pe, property1);
var outer = Expression.Property(inner, property2);


来源:https://stackoverflow.com/questions/11439218/how-can-i-create-a-expression-property-of-a-child-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!