티스토리 뷰

🌈 .NET MVC

enum 문자열 값 사용하기

James Wetzel 2018. 10. 25. 17:07
728x90
반응형
public enum State
{
    [Description("Karnataka")]
    KARNATAKA = 1,
    [Description("Gujarat")]
    GUJRAT = 2,
    [Description("Assam")]
    ASSAM = 3,
    [Description("Maharashtra")]
    MAHARASHTRA = 4,
    [Description("Goa")]
    GOA = 5
}
State stateVal = State.GOA;
string stateName = GetEnumDescription(stateVal);
int stateVal = 2;
string stateName = GetEnumDescription((State)stateVal);  
public static string GetEnumDescription(Enum enumVal)
{
    System.Reflection.MemberInfo[] memInfo = enumVal.GetType().GetMember(enumVal.ToString());
    DescriptionAttribute attribute = CustomAttributeExtensions.GetCustomAttribute<DescriptionAttribute>(memInfo[0]);
    return attribute.Description;
}


728x90
반응형