Summary of common operations of C # enumerated types
An enumerated type defines a set of symbolic name / value pairs. Enumerated types are strongly typed. Each enumerated type is derived from system.Enum and system.ValueType, while system.ValueType is derived from system.Object, so an enumerated type refers to a type.
When you compile an enumerated type, the C # compiler converts each symbol to a constant field of the type. The C # compiler treats enumerated types as primitive types.
1. Get the enumeration list:
/ get the type of enumeration list / enumeration list public static Dictionary GetEnumList (Type enumType) {var dic = new Dictionary (); try {var fd = enumType.GetFields (); for (var index = 1; index)
< fd.Length; ++index) { var info = fd[index]; var fieldValue = System.Enum.Parse(enumType, fd[index].Name); var attrs = info.GetCustomAttributes(typeof(EnumTextAttribute), false); foreach (EnumTextAttribute attr in attrs) { var key = (int)fieldValue; if (key == -100) continue; var value = attr.Text; dic.Add(key, value); } } return dic; } catch (Exception ex) { throw new Exception(ex.Message); } } 2.获取枚举名称: /// /// 获取枚举名称 /// /// 枚举的类型 /// 枚举值 /// 如果枚举值存在,返回对应的枚举名称,否则,返回空字符 public static string GetEnumTextById(Type enumType, int id) { var ret = string.Empty; try { var dic = GetEnumList(enumType); foreach (var item in dic) { if (item.Key != id) continue; ret = item.Value; break; } return ret; } catch (Exception ex) { throw new Exception(ex.Message); } } 3.根据枚举值获取对应中文描述:新航道培训 /// /// 根据枚举值获取对应中文描述 /// /// 枚举值 /// 枚举值中文描述 public static string GetEnumTextByEnum(object enumValue) { var ret = string.Empty; if ((int)enumValue == -1) return ret; try { var dic = GetEnumList(enumValue.GetType()); foreach (var item in dic) { if (item.Key != (int)enumValue) continue; ret = item.Value; break; } return ret; } catch (Exception ex) { throw new Exception(ex.Message); } } 4.获取枚举名称: /// /// 获取枚举名称 /// /// 枚举的类型 /// 枚举值的位置编号 /// 如果枚举值存在,返回对应的枚举名称,否则,返回空字符 public static string GetEnumTextByIndex(Type enumType, int index) { var ret = string.Empty; var dic = GetEnumList(enumType); if (index < 0 || index >Dic.Count) return ret; var i = 0; foreach (var item in dic) {if (I = = index) {ret = item.Value; break;} iTunes;} return ret }
5. Get enumerated values:
/ get the enumeration value / Enum name / if the enumeration name exists, return the corresponding enumeration value, otherwise, return-1 public static int GetEnumIdByName (Type enumType, string name) {var ret =-1 If (string.IsNullOrEmpty (name)) return ret; var dic = GetEnumList (enumType); foreach (var item in dic) {if (string.Compare (item.Value, name, StringComparison.Ordinal)! = 0) continue; ret = item.Key; break;} return ret }
6. Get the name corresponding enumeration value:
/ get the name corresponding enumeration value / enumerated type / enumerated name / public static T GetEnumIdByName (string name) where T: new () {var type = typeof (T); var enumItem = (T) TypeDescriptor.GetConverter (type) .ConvertFrom ("- 1") If (string.IsNullOrEmpty (name)) return enumItem; try {var fd = typeof (T). GetFields (); for (var index = 1; index < fd.Length; + + index) {var info = fd [index] Var fieldValue = System.Enum.Parse (type, FD [index] .Name); var attrs = info.GetCustomAttributes (typeof (EnumTextAttribute), false); if (attrs.Length! = 1) continue; var attr = (EnumTextAttribute) attrs [0]; if (! name.Equals (attr.Text)) continue EnumItem = (T) fieldValue; break;} return enumItem;} catch (Exception ex) {throw new Exception (ex.Message);}}
7. Get the location number of the enumerated value:
/ get the location number of the enumeration value / the type of the enumeration / enumeration name / if the enumeration name exists, return the location number of the corresponding enumeration value, otherwise, return-1 public static int GetEnumIndexByName (Type enumType, string name) {var ret =-1 If (string.IsNullOrEmpty (name)) return ret; var dic = GetEnumList (enumType); var I = 0; foreach (var item in dic) {if (string.Compare (item.Value, name, StringComparison.Ordinal) = = 0) {ret = I Break;} iTunes;} return ret;}