using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.IO.Ports; using System.Linq; using System.Reflection; using System.Text; namespace Cowain.Bake.Common.Enums { public static class EnumHelper { public static string FetchDescription(this Enum value) { try { FieldInfo fi = value.GetType().GetField(value.ToString()); if (null == fi) { return ""; } DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) return attributes[0].Description; else return value.ToString(); } catch { return ""; } } public static string GetEnumDescription(this Enum @enum, int value) { Type enumType = @enum.GetType(); if (Enum.IsDefined(enumType, value)) { DescriptionAttribute[] descriptAttr = enumType.GetField(Enum.GetName(enumType, value)).GetDescriptAttr(); return descriptAttr == null || descriptAttr.Length == 0 ? "" : descriptAttr[0].Description; } return "枚举异常"; } public static string GetDescription(this Enum enumName) { string str = string.Empty; DescriptionAttribute[] descriptAttr = enumName.GetType().GetField(enumName.ToString()).GetDescriptAttr(); return descriptAttr == null || descriptAttr.Length == 0 ? enumName.ToString() : descriptAttr[0].Description; } public static ArrayList ToArrayList(this Enum en) { ArrayList arrayList = new ArrayList(); foreach (Enum @enum in Enum.GetValues(en.GetType())) arrayList.Add(new KeyValuePair(@enum, @enum.GetDescription())); return arrayList; } public static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo) { if (fieldInfo != null) return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); return null; } public static dynamic Todynamic(this Enum en) { Dictionary valuePairs = new Dictionary(); foreach (Enum @enum in Enum.GetValues(en.GetType())) valuePairs.Add((int)Enum.Parse(typeof(T), @enum.ToString()), @enum.GetDescription()); return valuePairs; } public static dynamic ToStringKey(this Enum en) { Dictionary valuePairs = new Dictionary(); foreach (Enum @enum in Enum.GetValues(en.GetType())) valuePairs.Add(@enum.ToString(), @enum.GetDescription()); return valuePairs; } public static dynamic GetListDesc(this Enum en) { List lst = new List(); foreach (Enum @enum in Enum.GetValues(en.GetType())) lst.Add(@enum.GetDescription()); return lst; } public static dynamic ListKeyString(this Enum en) { List lst = new List(); foreach (Enum @enum in Enum.GetValues(en.GetType())) lst.Add(@enum.ToString()); return lst; } /// /// /// /// The enum type. /// The description. /// The enum value. public static EnumType GetValueByDescription(this string description) { var type = typeof(EnumType); foreach (var enumName in Enum.GetNames(type)) { var enumValue = Enum.Parse(type, enumName); if (description == ((Enum)enumValue).GetDescription()) return (EnumType)enumValue; } throw new ArgumentException("在指定的枚举类型值中没有此描述的值"); } public static EnumType GetValueByKey(this string name) { var type = typeof(EnumType); foreach (var enumName in Enum.GetNames(type)) { var enumValue = Enum.Parse(type, enumName); if (name == ((Enum)enumValue).ToString()) return (EnumType)enumValue; } throw new ArgumentException("在指定的枚举类型值中没有此描述的值"); } public static List GetDescriptions() where T : Enum { return Enum.GetValues(typeof(T)) .Cast() .Select(e => GetDescription(e)) .ToList(); } public static List GetEnumList() { var type = typeof(EnumType); List enumModels = new List(); foreach (var enumName in Enum.GetNames(type)) { EnumModel enumModel = new EnumModel(); enumModel.EnumString= enumName; var enumValue = Enum.Parse(type, enumName); enumModel.EnumDesc= ((Enum)enumValue).GetDescription(); enumModel.EnumIntValue = int.Parse(((Enum)enumValue).ToString("D")); enumModels.Add(enumModel); } return enumModels; } public static Parity GetParity(string name) { Parity parity = Parity.None; var arr = System.Enum.GetValues(typeof(Parity)); foreach (Parity item in arr) { if (item.ToString() == name) { parity = item; break; } } return parity; } public static StopBits GetStopBits(string name) { StopBits stopBits = StopBits.One; var arr = System.Enum.GetValues(typeof(StopBits)); foreach (StopBits item in arr) { if (item.ToString() == name) { stopBits = item; break; } } return stopBits; } public static Handshake GetHandshake(string name) { Handshake handshake = Handshake.None; var arr = System.Enum.GetValues(typeof(Handshake)); foreach (Handshake item in arr) { if (item.ToString() == name) { handshake = item; break; } } return handshake; } public static bool IsDefined(int value) where T : struct, Enum { return Enum.IsDefined(typeof(T), value); } //public static T GetNext(this T value) where T : struct, Enum //{ // var values = Enum.GetValues(typeof(T)).Cast().ToArray(); // int currentIndex = Array.IndexOf(values, value); // if (currentIndex < values.Length - 1) // { // return values[currentIndex + 1]; // } // else // { // return values[0]; // } //} public static void GetEnum(string a, ref T t) { foreach (T b in Enum.GetValues(typeof(T))) { if (GetDescription(b as Enum) == a) t = b; } } } public class EnumModel { public string EnumString { get; set; } public string EnumDesc { get; set; } public int EnumIntValue { get; set; } } }