Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of extended characteristics of Enum

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the example analysis of the extended features of Enum. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Use features instead of directly using Chinese as attributes. The specially extracted part is convenient for use in the future.

/ public static class EnumTools {/ get the description and sort of the current enumeration value / return the tuple Tuple (string,int) public static Tuple GetDescription (this Enum value) {int order = 0X string description = string.Empty; Type type = value.GetType (); / / get the enumeration FieldInfo fieldInfo = type.GetField (value.ToString ()) / / get enumerated custom properties DescriptionAttributeobject [] attrs = fieldInfo.GetCustomAttributes (typeof (DescriptionAttribute), false); DescriptionAttribute attr = (DescriptionAttribute) attrs.FirstOrDefault (a = > an is DescriptionAttribute); description = fieldInfo.Name;if (attr! = null) {order = attr.Order; description = attr.Name;} return new Tuple (description,order) } / get all descriptions of the current enumeration / public static List GetAll () {return GetAll (typeof (T));} / get all enumeration descriptions and values / public static List GetAll (Type type) {List list = new List () / / loop enumeration to get all Fieldsforeach (var field in type.GetFields ()) {/ / if it is enumerated type if (field.FieldType.IsEnum) {object tmp = field.GetValue (null); Enum enumValue = (Enum) tmp;int intValue = Convert.ToInt32 (enumValue); var dec = enumValue.GetDescription (); int order= dec.Item2;string showName = dec.Item1 / / get description and sort list.Add (new EnumToolsModel {Key = intValue, Name = showName, Order = order});}} / / sort and convert to KeyValue return return list.OrderBy (I = > i.Order) .Select (I = > new KeyValuePair (i.Key, i.Name)). ToList ();} / enumerate Model/// partial class EnumToolsModel {public int Order {get; set } public string Name {get; set;} public int Key {get; set;}} / enumerate features / [AttributeUsage (AttributeTargets.Field, AllowMultiple = false, Inherited = false)] public class DescriptionAttribute: Attribute {/ sort / public int Order {get; set;} / name / public string Name {get; set } / define description name / name public DescriptionAttribute (string name) {Name = name;} / define description name and sort / name / sort public DescriptionAttribute (string name, int order) {Name = name; Order = order;}

Replace the out parameter in the original text with the return tuple, because the project is developed by vs2015, you can't use the cantilever 7.0 feature, otherwise it would be better to use the value tuple in 7.0. Performance and display friendliness will be improved.

This is the end of this article on "sample analysis of Enum extension features". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report