在C#中读取枚举值的描述属性

枚举:
public enum EnumLanugage
{
    [System.ComponentModel.Description("中文")]
    Chinese,
    English
}

获取值描述的方法:
public string GetEnumDescription(Enum enumValue)
{
    string str = enumValue.ToString();
    System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
    object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
    if (objs == null || objs.Length == 0) return str;
    System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
    return da.Description;
}

调用 GetEnumDescription(EnumLanguage.Chinese) 后 将返回“中文”

  

原文地址:https://www.cnblogs.com/XuPengLB/p/6243775.html