using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OrpaonEMS.App.Com { public static class EnumExtension { #region 获取枚举描述内容 public static Attribute GetEnumAttribute(this Enum value, Type attribute) { var enumType = value.GetType(); var name = Enum.GetName(enumType, value); if (name != null) { // 获取枚举字段。 var fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 获取描述的属性。 var attr = Attribute.GetCustomAttribute(fieldInfo, attribute, false); return attr; } } return null; } public static T GetEnumAttribute(this Enum value) { var enumType = value.GetType(); var name = Enum.GetName(enumType, value); if (name != null) { // 获取枚举字段。 var fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 获取描述的属性。 var attr = Attribute.GetCustomAttribute(fieldInfo, typeof(T), false); return (T)(object)attr; } } return default(T); } /// /// 获取枚举描述内容; /// /// /// /// public static string GetEnumDescription(this Enum value, string defaultval = "") { var attr = GetEnumAttribute(value, typeof(DescriptionAttribute)); return (attr as DescriptionAttribute)?.Description ?? defaultval; } #endregion #region 获取枚举列表 /// /// 通过枚举对象获取枚举列表 /// /// /// /// public static List GetEnumList(this T value) { var list = new List(); if (value is Enum) { var valData = Convert.ToInt32((T)Enum.Parse(typeof(T), value.ToString())); var tps = Enum.GetValues(typeof(T)); list.AddRange(from object tp in tps where ((int)Convert.ToInt32((T)Enum.Parse(typeof(T), tp.ToString())) & valData) == valData select (T)tp); } return list; } /* 参考:https://www.codenong.com/17123548/ */ /// /// 通过枚举类型获取枚举列表; /// /// /// /// public static List GetEnumList() where T : Enum { List list = Enum.GetValues(typeof(T)).OfType().ToList(); return list; } /* 参考:https://www.codenong.com/105372/ */ /// /// Gets all items for an enum value.(通过枚举对象获取所有枚举) /// /// /// The value. /// public static IEnumerable GetAllItems(this Enum value) { foreach (object item in Enum.GetValues(typeof(T))) { yield return (T)item; } } /// /// Gets all items for an enum type.(通过枚举类型获取所有枚举) /// /// /// public static IEnumerable GetAllItems() where T : struct { foreach (object item in Enum.GetValues(typeof(T))) { yield return (T)item; } } #endregion /// /// 将枚举转换为值 /// /// /// /// public static T ToValue(this Enum value) where T : struct { return (T)(object)value; } } }