如何创建子对象的Expression.Property

时间:2012-07-11 18:39:11

标签: c# linq expression-trees expression func

通常我会以这种方式创建一个表达式。

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

string Name = "property1";

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

它产生left = x => x.property1

我需要知道如何制作

left = x => x.Object2.property1

如果Name =“Object2.property1”;和object2是object1的子项

提前致谢

1 个答案:

答案 0 :(得分:27)

我不太明白你想要什么。它是一个属性链(例如: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);