Skip to content

Instantly share code, notes, and snippets.

@pregress
Created May 24, 2013 09:13
Show Gist options
  • Save pregress/5642315 to your computer and use it in GitHub Desktop.
Save pregress/5642315 to your computer and use it in GitHub Desktop.
Get a property name by expression (Type safe)
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