通过Description特性获取枚举中文注释
枚举
public enum LanguageTypeEnum
{
/// <summary>
/// 未知
/// </summary>
[Description("未知")]
UnKnown = 0,
/// <summary>
/// 中文
/// </summary>
Chinese = 1,
}
新增一个枚举扩展方法
public static class EnumHelper
{
public static string GetEnumDiscriptionExtension(this Enum value)
{
var ret = string.Empty;
var enumType = value.GetType();
var filed = enumType.GetField(value.ToString());
if (filed.IsDefined(typeof(DescriptionAttribute),false))
{
//var des=(DescriptionAttribute)fileds.GetCustomAttribute(typeof(DescriptionAttribute),false);
var des = (DescriptionAttribute)field.GetCustomAttributes(typeof(DescriptionAttribute), false)[0];
return des.Description;
}
return ret;
}
}
使用Demo
static void Main(string[] args)
{
Console.WriteLine(LanguageTypeEnum.UnKnown.GetEnumDiscriptionExtension());//未知
Console.ReadLine();
}