Created
June 13, 2018 09:12
-
-
Save MichaelPolla/0cc6a6f067f04709824c9446d51f2a5d to your computer and use it in GitHub Desktop.
Get the name of the provided member (C# < 6.0)
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
/// <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