C # Summary of common enumeration operations,
The enumeration type defines a set of "symbol name/value" pairs. The enumeration type is strongly typed. Each Enumeration type is derived from system. Enum and system. ValueType, while system. ValueType is derived from system. Object. Therefore, the enumeration type refers to the type.
When compiling enumeration types, the C # compiler converts each symbol into a constant field of the type. C # The Compiler regards the enumeration type as a primitive type.
1. Retrieve the enumerated list:
/// <Summary> /// obtain the enumeration list /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <returns> enumeration list </returns> public static Dictionary <int, string> GetEnumList (Type enumType) {var dic = new Dictionary <int, string> (); 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. Obtain the enumerated Name:
/// <Summary> /// obtain the enumeration name /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <param name = "id"> enumeration value </param> // <returns> If the enumerated value exists, returns the corresponding enumerated name. Otherwise, null characters </returns> 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. obtain the corresponding Chinese Description Based on the enumerated values:
/// <Summary> /// obtain the corresponding Chinese Description Based on the enumerated value /// </summary> /// <param name = "enumValue"> enumeration value </param> /// <returns> description of enumerated values in Chinese </returns> 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. Obtain the enumerated Name:
/// <Summary> /// obtain the enumeration name /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <param name = "index"> Location Number of the enumerated value </param> // <returns> If the enumerated value exists, returns the corresponding enumerated name. Otherwise, null characters </returns> 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;} I ++;} return ret ;}
5. Obtain the enumerated values:
/// <Summary> /// obtain the enumerated value /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <param name = "name"> enumeration name </param> // <returns> If the enumeration name exists, returns the corresponding enumerated value. Otherwise,-1 </returns> 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. Obtain the enumerated values corresponding to the name:
/// <Summary> /// obtain the enumerated value corresponding to the name /// </summary> /// <typeparam name = "T"> Enumeration type </typeparam> // /<param name = "name"> enumeration name </param> // <returns> </returns> public static T GetEnumIdByName <T> (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 in Dex = 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. Obtain the position number of the enumerated value:
/// <Summary> /// obtain the ID of the location where the enumerated value is located /// </summary> /// <param name = "enumType"> Enumeration type </param> /// <param name = "name"> enumeration name </param> /// <returns> If the enumeration name exists, returns the Location Number of the corresponding enumerated value. Otherwise, returns-1 </returns> 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;} I ++;} return ret ;}