Due to the inherent flaw in English, I always use Chinese when enumerating, so that I don’t need to read the comments to know the meaning of the enumeration. Today I saw the blog post
Using attributes instead of directly using Chinese as attributes. The part is specially excerpted for future use


/// <summary>/// 枚举帮助类/// </summary>public static class EnumTools {/// <summary>/// 获取当前枚举值的描述和排序/// </summary>/// <param>/// <returns>返回元组Tuple(string,int)</returns>public static Tuple<string> GetDescription(this Enum value) {int order = 0;string description = string.Empty; Type type = value.GetType();// 获取枚举FieldInfo fieldInfo = type.GetField(value.ToString());// 获取枚举自定义的特性DescriptionAttributeobject[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute); description = fieldInfo.Name;if (attr != null) { order = attr.Order; description = attr.Name; }return new Tuple<string>(description,order); }/// <summary>/// 获取当前枚举的所有描述/// </summary>/// <returns></returns>public static List<keyvaluepair>> GetAll<t>() {return GetAll(typeof(T)); }/// <summary>/// 获取所有的枚举描述和值/// </summary>/// <param>/// <returns></returns>public static List<keyvaluepair>> GetAll(Type type) { List<enumtoolsmodel> list = new List<enumtoolsmodel>();// 循环枚举获取所有的Fieldsforeach (var field in type.GetFields()) {// 如果是枚举类型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; // 获取描述和排序list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order }); } }// 排序并转成KeyValue返回return list.OrderBy(i => i.Order).Select(i => new KeyValuePair<int>(i.Key, i.Name)).ToList(); }/// <summary>/// 枚举Model/// </summary> partial class EnumToolsModel {public int Order { get; set; }public string Name { get; set; }public int Key { get; set; } } }/// <summary>/// 枚举特性/// </summary>[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]public class DescriptionAttribute : Attribute {/// <summary>/// 排序/// </summary>public int Order { get; set; }/// <summary>/// 名称/// </summary>public string Name { get; set; }/// <summary>/// 定义描述名称/// </summary>/// <param>名称public DescriptionAttribute(string name) { Name = name; }/// <summary>/// 定义描述名称和排序/// </summary>/// <param>名称/// <param>排序public DescriptionAttribute(string name, int order) { Name = name; Order = order; } }</int></enumtoolsmodel></enumtoolsmodel></keyvaluepair></t></keyvaluepair></string></string>
Replace the out parameter in the original text with the return element Group, since the project is developed in vs2015, it cannot use c#7.0 features, otherwise it would be better to use the value tuple in 7.0. Performance and display friendliness will be improved.
The above is the detailed content of Enum extended feature example code. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool