Given the following types:
class Parent { List<Child> Children {get;set;}}
class Child {List<Child> GrandChildren {get;set;}}
class Helper<TEntity> {List<string> Properties {get;set;}}
And given the following methods on Helper...
public Helper AddProps<TEntity, TProp>(Expression<Func<TEntity, TProp>> exp)
{
this.Properties.Add(GetPropInfo(exp).Name);
}
public PropertyInfo GetPropInfo(Expression<Func<TEntity, TProp>> exp)
{
return (PropertyInfo)((MemberExpression)(expression.Body)).Member;
}
I am able to do this:
Helper<Parent> myHelper = new Helper<Parent>();
myHelper.AddProps(x => x.Children);
The string list "Properties" on myHelper would then contain the value "Children", the name of the property passed through the expression.
What I want to do now is to be able to achieve the same thing, only with the ability to reflect type hierarchy.
Would it look like this ?
x => x.Children { xx => xx.GrandChildren }
Or is it even possible, and what would be involved? I've seen nested lambda's before but don't know what's involved.
Thanks in advance!
EDIT
It seems there is some confusion so I'll try to clarify. I want to be able to create a string that looks like this "Object.SubObject.SubSubObject" using lambda expressions and method chaining. My example does this, but only for one level deep ( a property of a class). What I want to do is extend this to go to any depth.
For Example, I'd like to use lambda expressions with a fluent interface that would look something like this....
AddProps(x => x.Children).AddProps(xx => xx.GrandChildren) and that would add "Children.GrandChildren" to my "Properties" string list.