Skip to content

Instantly share code, notes, and snippets.

@rschiefer
Last active July 27, 2018 19:48
Show Gist options
  • Save rschiefer/a5d6cd715f6978af8c34885b05e0fb9a to your computer and use it in GitHub Desktop.
Save rschiefer/a5d6cd715f6978af8c34885b05e0fb9a to your computer and use it in GitHub Desktop.
public static string GetName<T>(this Expression<Func<T>> action)
{
return GetNameFromMemberExpression(action.Body);
}
static string GetNameFromMemberExpression(Expression expression) {
if (expression is MemberExpression) {
return (expression as MemberExpression).Member.Name;
}
else if (expression is UnaryExpression) {
return GetNameFromMemberExpression((expression as UnaryExpression).Operand);
}
return "MemberNameUnknown";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment