Skip to content

Instantly share code, notes, and snippets.

@MichaelPolla
Created June 13, 2018 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelPolla/0cc6a6f067f04709824c9446d51f2a5d to your computer and use it in GitHub Desktop.
Save MichaelPolla/0cc6a6f067f04709824c9446d51f2a5d to your computer and use it in GitHub Desktop.
Get the name of the provided member (C# < 6.0)
/// <summary>
/// Get the name of the provided member.
/// Same as using the operatore nameof provided by C# >= 6.0.
///
/// Usage: GetMemberName(() => myMember);
/// </summary>
/// <param name="memberExpression"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static string GetMemberName<T>(Expression<Func<T>> memberExpression)
{
// Source: https://stackoverflow.com/a/9801735/1975002
// Note: with C# >= 6.0, the operator nameof could be used instead.
MemberExpression expressionBody = (MemberExpression)memberExpression.Body;
return expressionBody.Member.Name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment