枚举大家都知道怎么用,但是今天遇到了一个问题,使用普通的枚举解决不了问题。业务环境是这样的,比如有很多命令,命令的格式都是#ETSDWd225,表示停止,等等若干个这样的命令,这时我们可以建立一个字典key,value去存储它,但是调用的时候就不是很方便,枚举在调用的时候,可以使用点,点出来,非常的方便,希望使用这个优点来解决这个问题。
环境vs2022
1,建立一个控制台程序,再建立一个水果枚举类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp3
- {
- public enum Fruit
- {
- apple,
- peach,
- watermelon,
- banana,
- orange
- }
- }
2,代码调用
- using ConsoleApp3;
-
- Console.WriteLine(Fruit.orange);
- Console.WriteLine("Hello, World!");
3,但是当我们要输出中文的时候,这个时候就要进行变化,使用特性
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp3
- {
- public enum Fruit
- {
- [Description("苹果")]
- apple,
- [Description("桃子")]
- peach,
- [Description("西瓜")]
- watermelon,
- [Description("香蕉")]
- banana,
- [Description("橘子")]
- orange
- }
- static class EnumExtensions
- {
- public static string GetDescription(this Enum val)
- {
- var field = val.GetType().GetField(val.ToString());
- if (field == null)
- return val.ToString();
- var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
- return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
-
- }
- }
- }
4.代码调用
- using ConsoleApp3;
-
- Console.WriteLine(Fruit.orange);
- Console.WriteLine(Fruit.orange.GetDescription());
-
- Console.WriteLine("Hello, World!");
5.效果

6.如果我们有很多水果,并且把水果进行分类,代码如下
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp3
- {
- public enum Fruit
- {
- [levelOne("一级")]
- [Description("苹果")]
- apple,
- [levelOne("二级")]
- [Description("桃子")]
- peach,
- [Description("西瓜")]
- watermelon,
- [levelOne("三级")]
- [Description("香蕉")]
- banana,
- [levelOne("三级")]
- [Description("橘子")]
- orange
- }
- static class EnumExtensions
- {
- public static string GetDescription(this Enum val)
- {
- var field = val.GetType().GetField(val.ToString());
- if (field == null)
- return val.ToString();
- var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
- return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
- }
- public static string Getlevel(this Enum val)
- {
- var field = val.GetType().GetField(val.ToString());
- if(field == null)
- return val.ToString();
- var customAttribute = Attribute.GetCustomAttribute(field, typeof(levelOne));
- return customAttribute == null ? val.ToString() : ((levelOne)customAttribute).Data;
- }
- }
- public class levelOne : Attribute
- {
- public string Data { get; set; }
- public levelOne() { }
- public levelOne(string data)
- {
- Data = data;
- }
- }
-
- }
7. 代码调用
- using ConsoleApp3;
-
- Console.WriteLine(Fruit.orange);
- Console.WriteLine(Fruit.orange.GetDescription());
- Console.WriteLine(Fruit.orange.Getlevel());
- Console.WriteLine(Fruit.apple.Getlevel());
- Console.WriteLine("Hello, World!");
8.效果

拓展
接下来,我们看本文最初提到的问题
比如有很多命令,命令的格式都是#ETSDWd225,表示停止,我们随便写几个命令来表示业务场景中的命令
#ETSDWd225 停止
#ETSDWd226 向上
#ETSDWd227 向下
#ETSDWd228 复原
#ETSDWd229 关闭
此时,我们看看这个枚举类型,应该怎么写呢?

我们可以把命令写在描述里面,把注释写在枚举中,代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp3
- {
- public enum Fruit
- {
- [levelOne("一级")]
- [Description("#ETSDWd225")]
- 停止,
- [levelOne("二级")]
- [Description("#ETSDWd226")]
- 向上,
- [Description("#ETSDWd227")]
- 向下,
- [levelOne("三级")]
- [Description("#ETSDWd228")]
- 复原,
- [levelOne("三级")]
- [Description("#ETSDWd229")]
- 关闭
- }
- static class EnumExtensions
- {
- public static string GetDescription(this Enum val)
- {
- var field = val.GetType().GetField(val.ToString());
- if (field == null)
- return val.ToString();
- var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
- return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
- }
- public static string Getlevel(this Enum val)
- {
- var field = val.GetType().GetField(val.ToString());
- if(field == null)
- return val.ToString();
- var customAttribute = Attribute.GetCustomAttribute(field, typeof(levelOne));
- return customAttribute == null ? val.ToString() : ((levelOne)customAttribute).Data;
- }
- }
- public class levelOne : Attribute
- {
- public string Data { get; set; }
- public levelOne() { }
- public levelOne(string data)
- {
- Data = data;
- }
- }
-
- }
调用
- using ConsoleApp3;
-
- Console.WriteLine(Fruit.复原);
- Console.WriteLine(Fruit.向上.GetDescription());
- Console.WriteLine(Fruit.向下.Getlevel());
- Console.WriteLine(Fruit.关闭.Getlevel());
- Console.WriteLine("Hello, World!");
效果

这里的#ETSDWd226就是我们真正需要的值。