using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace CK.SCP.Utils { public class EnumItem { public EnumItem(string name, string desc, int value) { Name = name; Desc = desc; Value = value; } public EnumItem() { } public string Name { get; set; } public string Desc { get; set; } public int Value { get; set; } } public static class EnumHelper { public static List EnumToList() { List list = new List(); var type = typeof(T); foreach (var name in Enum.GetNames(type)) { var text = GetText(type, name); int value = (int) Enum.Parse(type, name); list.Add(new EnumItem (name,text,value)); } return list; } public static Dictionary EnumToDict() { Dictionary dictionary = new Dictionary(); var type = typeof (T); foreach (var name in Enum.GetNames(type)) { var text = GetText(type, name); dictionary.Add(text, (int) Enum.Parse(type, name)); } return dictionary; } private static string GetText(Type type, string name) { var fieldInfo = type.GetField(name); var text = name; if (fieldInfo != null) { // 获取描述的属性。 DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof (DescriptionAttribute), false) as DescriptionAttribute; if (attr != null) { text = attr.Description; } } return text; } public static string GetDescription(Enum value) { Type enumType = value.GetType(); // 获取枚举常数名称。 var name = Enum.GetName(enumType, value); if (name == null) return null; // 获取枚举字段。 var fieldInfo = enumType.GetField(name); if (fieldInfo == null) return null; // 获取描述的属性。 var attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; return attr?.Description; } public static string GetDesc(Enum value) { Type enumType = value.GetType(); // 获取枚举常数名称。 var name = Enum.GetName(enumType, value); if (name == null) return string.Empty; // 获取枚举字段。 var fieldInfo = enumType.GetField(name); if (fieldInfo == null) return string.Empty; // 获取描述的属性。 var attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; return attr?.Description; } } }