Created
November 16, 2013 11:11
-
-
Save simon-reynolds/7498983 to your computer and use it in GitHub Desktop.
Extension methods for getting name of property as a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string GetPropertyName<T>(this T type, Expression<Func<T, object>> expression) | |
{ | |
return GetPropertyName<T>(expression); | |
} | |
public static string GetPropertyName<T>(Expression<Func<T, object>> expression) | |
{ | |
var lambda = expression as LambdaExpression; | |
MemberExpression memberExpression = lambda.Body is UnaryExpression | |
? (lambda.Body as UnaryExpression).Operand as MemberExpression | |
: lambda.Body as MemberExpression; | |
return memberExpression == null ? null : (memberExpression.Member as PropertyInfo).Name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment