How can I Create a Expression.Property of a child object
Asked Answered
B

1

11

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

Baskett answered 11/7, 2012 at 18:39 Comment(0)
C
40

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);
Clergy answered 11/7, 2012 at 19:13 Comment(2)
Thanks actually this is what I needed. I can't vote yet. but this is the answerBaskett
Can you set an answer to your own question as valid?Clergy

© 2022 - 2024 — McMap. All rights reserved.