Created
May 24, 2013 09:13
-
-
Save pregress/5642315 to your computer and use it in GitHub Desktop.
Get a property name by expression (Type safe)
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
protected string GetPropertyName<T>(Expression<Func<T>> propertyExpression) | |
{ | |
if (propertyExpression == null) | |
{ | |
throw new ArgumentNullException("propertyExpression"); | |
} | |
var body = propertyExpression.Body as MemberExpression; | |
if (body == null) | |
{ | |
throw new ArgumentException("Invalid argument", "propertyExpression"); | |
} | |
var property = body.Member as PropertyInfo; | |
if (property == null) | |
{ | |
throw new ArgumentException("Argument is not a property", "propertyExpression"); | |
} | |
return property.Name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment