c# enum 解析

解析定义的枚举

1
2
3
4
5
6
7
8
9
10
11
12
13
public enum OrderPaymentStatus
   {
       /// <summary>
       /// 未支付
       /// </summary>
       [Description("未支付")]
       No=1,
       /// <summary>
       /// 已支付
       /// </summary>
       [Description("已支付")]
       Yes
   }

解析类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public static class EnumHelper
    {
        private static Hashtable enumDesciption = EnumHelper.GetDescriptionContainer();
 
        public static string ToDescription(this Enum value)
        {
            if (value == null)
            {
                return "";
            }
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            return EnumHelper.GetDescription(type, name);
        }
 
        public static Dictionary<int, string> ToDescriptionDictionary<TEnum>()
        {
            Type typeFromHandle = typeof(TEnum);
            Array values = Enum.GetValues(typeFromHandle);
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (Enum item in values)
            {
                dictionary.Add(Convert.ToInt32(item), item.ToDescription());
            }
            return dictionary;
        }
 
        public static Dictionary<int, string> ToDictionary<TEnum>()
        {
            Type typeFromHandle = typeof(TEnum);
            Array values = Enum.GetValues(typeFromHandle);
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (Enum item in values)
            {
                dictionary.Add(Convert.ToInt32(item), item.ToString());
            }
            return dictionary;
        }
 
        private static bool IsIntType(double d)
        {
            return (double)(int)d != d;
        }
 
        private static Hashtable GetDescriptionContainer()
        {
            EnumHelper.enumDesciption = new Hashtable();
            return EnumHelper.enumDesciption;
        }
 
        private static void AddToEnumDescription(Type enumType)
        {
            EnumHelper.enumDesciption.Add(enumType, EnumHelper.GetEnumDic(enumType));
        }
 
        private static Dictionary<string, string> GetEnumDic(Type enumType)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            FieldInfo[] fields = enumType.GetFields();
            FieldInfo[] array = fields;
            foreach (FieldInfo fieldInfo in array)
            {
                if (fieldInfo.FieldType.IsEnum)
                {
                    object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    dictionary.Add(fieldInfo.Name, ((DescriptionAttribute)customAttributes[0]).Description);
                }
            }
            return dictionary;
        }
 
        private static string GetDescription(Type enumType, string enumText)
        {
            if (string.IsNullOrEmpty(enumText))
            {
                return null;
            }
            if (!EnumHelper.enumDesciption.ContainsKey(enumType))
            {
                EnumHelper.AddToEnumDescription(enumType);
            }
            object obj = EnumHelper.enumDesciption[enumType];
            if (obj != null && !string.IsNullOrEmpty(enumText))
            {
                Dictionary<string, string> dictionary = (Dictionary<string, string>)obj;
                return dictionary[enumText].Split('|')[0];
            }
            throw new ApplicationException("不存在枚举的描述");
        }
    }

  

posted on   落叶子  阅读(346)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示