• c#枚举使用


    枚举大家都知道怎么用,但是今天遇到了一个问题,使用普通的枚举解决不了问题。业务环境是这样的,比如有很多命令,命令的格式都是#ETSDWd225,表示停止,等等若干个这样的命令,这时我们可以建立一个字典key,value去存储它,但是调用的时候就不是很方便,枚举在调用的时候,可以使用点,点出来,非常的方便,希望使用这个优点来解决这个问题。

    环境vs2022

    1,建立一个控制台程序,再建立一个水果枚举类

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace ConsoleApp3
    7. {
    8. public enum Fruit
    9. {
    10. apple,
    11. peach,
    12. watermelon,
    13. banana,
    14. orange
    15. }
    16. }

    2,代码调用

    1. using ConsoleApp3;
    2. Console.WriteLine(Fruit.orange);
    3. Console.WriteLine("Hello, World!");

     3,但是当我们要输出中文的时候,这个时候就要进行变化,使用特性

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Reflection;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace ConsoleApp3
    9. {
    10. public enum Fruit
    11. {
    12. [Description("苹果")]
    13. apple,
    14. [Description("桃子")]
    15. peach,
    16. [Description("西瓜")]
    17. watermelon,
    18. [Description("香蕉")]
    19. banana,
    20. [Description("橘子")]
    21. orange
    22. }
    23. static class EnumExtensions
    24. {
    25. public static string GetDescription(this Enum val)
    26. {
    27. var field = val.GetType().GetField(val.ToString());
    28. if (field == null)
    29. return val.ToString();
    30. var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
    31. return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
    32. }
    33. }
    34. }

    4.代码调用

    1. using ConsoleApp3;
    2. Console.WriteLine(Fruit.orange);
    3. Console.WriteLine(Fruit.orange.GetDescription());
    4. Console.WriteLine("Hello, World!");

    5.效果

     6.如果我们有很多水果,并且把水果进行分类,代码如下

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Reflection;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace ConsoleApp3
    9. {
    10. public enum Fruit
    11. {
    12. [levelOne("一级")]
    13. [Description("苹果")]
    14. apple,
    15. [levelOne("二级")]
    16. [Description("桃子")]
    17. peach,
    18. [Description("西瓜")]
    19. watermelon,
    20. [levelOne("三级")]
    21. [Description("香蕉")]
    22. banana,
    23. [levelOne("三级")]
    24. [Description("橘子")]
    25. orange
    26. }
    27. static class EnumExtensions
    28. {
    29. public static string GetDescription(this Enum val)
    30. {
    31. var field = val.GetType().GetField(val.ToString());
    32. if (field == null)
    33. return val.ToString();
    34. var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
    35. return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
    36. }
    37. public static string Getlevel(this Enum val)
    38. {
    39. var field = val.GetType().GetField(val.ToString());
    40. if(field == null)
    41. return val.ToString();
    42. var customAttribute = Attribute.GetCustomAttribute(field, typeof(levelOne));
    43. return customAttribute == null ? val.ToString() : ((levelOne)customAttribute).Data;
    44. }
    45. }
    46. public class levelOne : Attribute
    47. {
    48. public string Data { get; set; }
    49. public levelOne() { }
    50. public levelOne(string data)
    51. {
    52. Data = data;
    53. }
    54. }
    55. }

    7. 代码调用

    1. using ConsoleApp3;
    2. Console.WriteLine(Fruit.orange);
    3. Console.WriteLine(Fruit.orange.GetDescription());
    4. Console.WriteLine(Fruit.orange.Getlevel());
    5. Console.WriteLine(Fruit.apple.Getlevel());
    6. Console.WriteLine("Hello, World!");

    8.效果

    拓展

    接下来,我们看本文最初提到的问题

    比如有很多命令,命令的格式都是#ETSDWd225,表示停止,我们随便写几个命令来表示业务场景中的命令

    #ETSDWd225  停止

    #ETSDWd226  向上

    #ETSDWd227  向下

    #ETSDWd228  复原

    #ETSDWd229  关闭

    此时,我们看看这个枚举类型,应该怎么写呢?

    我们可以把命令写在描述里面,把注释写在枚举中,代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Linq;
    5. using System.Reflection;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace ConsoleApp3
    9. {
    10. public enum Fruit
    11. {
    12. [levelOne("一级")]
    13. [Description("#ETSDWd225")]
    14. 停止,
    15. [levelOne("二级")]
    16. [Description("#ETSDWd226")]
    17. 向上,
    18. [Description("#ETSDWd227")]
    19. 向下,
    20. [levelOne("三级")]
    21. [Description("#ETSDWd228")]
    22. 复原,
    23. [levelOne("三级")]
    24. [Description("#ETSDWd229")]
    25. 关闭
    26. }
    27. static class EnumExtensions
    28. {
    29. public static string GetDescription(this Enum val)
    30. {
    31. var field = val.GetType().GetField(val.ToString());
    32. if (field == null)
    33. return val.ToString();
    34. var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)); //使用反射,反射效率低
    35. return customAttribute == null ? val.ToString() : ((DescriptionAttribute)customAttribute).Description;
    36. }
    37. public static string Getlevel(this Enum val)
    38. {
    39. var field = val.GetType().GetField(val.ToString());
    40. if(field == null)
    41. return val.ToString();
    42. var customAttribute = Attribute.GetCustomAttribute(field, typeof(levelOne));
    43. return customAttribute == null ? val.ToString() : ((levelOne)customAttribute).Data;
    44. }
    45. }
    46. public class levelOne : Attribute
    47. {
    48. public string Data { get; set; }
    49. public levelOne() { }
    50. public levelOne(string data)
    51. {
    52. Data = data;
    53. }
    54. }
    55. }

    调用

    1. using ConsoleApp3;
    2. Console.WriteLine(Fruit.复原);
    3. Console.WriteLine(Fruit.向上.GetDescription());
    4. Console.WriteLine(Fruit.向下.Getlevel());
    5. Console.WriteLine(Fruit.关闭.Getlevel());
    6. Console.WriteLine("Hello, World!");

    效果

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

  • 相关阅读:
    关于接口测试问题,这些文章推荐你反复看看
    基因组大小查询(二)|基因组组装结果查询
    最新AI系统ChatGPT源码+支持OpenAI全模型+国内AI模型+AI绘画
    webpack 打包优化 - splitChunks
    [自制操作系统] 第12回 实现中断代码
    模式分类与“组件协作模式”
    环信集成SDK
    matlab求解实际问题
    FlinkSQL系列07-表查询
    线程池的使用
  • 原文地址:https://blog.csdn.net/u012563853/article/details/126293420