lundi 22 juin 2015

How can I recursively return the reflected names of a property and all its parents

So I have the following helper method that is returning the name of a property given an expression. Its being used in an MVC application to set the ID of a hidden field in a view based on the property it needs to bind to .

public static string GetIDFromPropertyName<T>(Expression<Func<T>> exp)
{
   return (((MemberExpression)(exp.Body)).Member).Name;
}

However what I want is to be able to pass in an expression such as:

GetIDFromPropertyName(() => model.AComplexProperty.AnotherComplexProperty.ASimpleProperty)

And have returned back to me:

AComplexProperty_AnotherComplexProperty_ASimpleProperty

How can I do this?





Aucun commentaire:

Enregistrer un commentaire