Created
November 2, 2015 05:38
-
-
Save gongdo/7aa66016d8f4a6c4ccc9 to your computer and use it in GitHub Desktop.
EnumMemberAttributeExtensions
This file contains hidden or 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> | |
/// EnumMemberAttribute에 관련된 확장 메서드 | |
/// </summary> | |
public static class EnumMemberAttributeExtensions | |
{ | |
/// <summary> | |
/// 주어진 enum 값에 선언된 EnumMemberAttribute의 Value를 반환합니다. | |
/// EnumMemberAttribute가 없을 경우 enum 값을 ToString()하여 반환합니다. | |
/// </summary> | |
/// <param name="enumeration">enum 값</param> | |
/// <returns>EnumMemberAttribute로 정의한 Value.</returns> | |
public static string GetMemberValue(this Enum enumeration) | |
{ | |
return enumeration.GetType().GetTypeInfo() | |
.DeclaredMembers | |
.FirstOrDefault(m => m.Name == enumeration.ToString()) | |
?.GetCustomAttribute<EnumMemberAttribute>() | |
?.Value | |
?? enumeration.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment