In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
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;}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
Export all rules to text netsh ipsec static show all > ipsec.txt
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.