/// <summary> /// 枚举测试 /// </summary> public enum TestTypeEnum { [Description("书籍")]Book = 1, [Description("手机")]Phone = 2 }
public static class EnumHelp { public static string GetDescription(this Enum value) { Type enumType = value.GetType(); string name = Enum.GetName(enumType, value); if (name != null) { FieldInfo fieldInfo = enumType.GetField(name); if (fieldInfo != null) { if (Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) is DescriptionAttribute attr) { return attr.Description; } } } return null; } }
var dec = TestTypeEnum.Book.GetDescription();
原文地址:https://www.cnblogs.com/dayang12525/p/11066076.html
时间: 2024-11-11 06:44:28