Given a Member Access lambda expression, convert it to a specific string representation with full ac

Posted by Nathan on Stack Overflow See other posts from Stack Overflow or by Nathan
Published on 2010-06-15T23:35:15Z Indexed on 2010/06/15 23:42 UTC
Read the original article Hit count: 273

Filed under:
|
|

Given an

Expression<Func<T, object>> 

(e.g. x => x.Prop1.SubProp), I want to create a string "Prop1.SubProp" for as deep as necessary.

In the case of a single access (e.g. x => x.Prop1), I can easily do this with:

MemberExpression body = (expression.Body.NodeType == ExpressionType.Convert) ? (MemberExpression)((UnaryExpression)expression.Body).Operand : (MemberExpression)expression.Body;
return body.Member.Name;

However, if there is deeper nesting, e.g. x => x.Prop1.SubProp1, this only gets the most deeply nested name, e.g. "SubProp1" instead of "Prop1.SubProp1"

Is there anyway to access the full property path of a lambda expression?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET