前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Enum扩展特性,代替中文属性

Enum扩展特性,代替中文属性

作者头像
用户6362579
发布2019-09-29 16:51:00
4030
发布2019-09-29 16:51:00
举报
文章被收录于专栏:小神仙

枚举特性类:

代码语言:javascript
复制
    /// <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 name="name">名称</param>
        public DescriptionAttribute(string name)
        {
            Name = name;
        }

        /// <summary>
        /// 定义描述名称和排序
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="order">排序</param>
        public DescriptionAttribute(string name, int order)
        {
            Name = name;
            Order = order;
        }

    }

枚举帮助类:

代码语言:javascript
复制
 1     /// <summary>
 2     /// 枚举帮助类
 3     /// </summary>
 4     public static class EnumTools
 5     {
 6         /// <summary>
 7         ///  获取当前枚举值的描述和排序
 8         /// </summary>
 9         /// <param name="value"></param>
10         /// <returns>返回元组Tuple(string,int)</returns>
11         public static Tuple<string, int> GetDescription(this Enum value)
12         {
13             int order = 0;
14             string description = string.Empty;
15 
16             Type type = value.GetType();
17 
18             // 获取枚举
19             FieldInfo fieldInfo = type.GetField(value.ToString());
20 
21             // 获取枚举自定义的特性DescriptionAttribute
22             object[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
23             DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute);
24 
25             description = fieldInfo.Name;
26 
27             if (attr != null)
28             {
29                 order = attr.Order;
30                 description = attr.Name;
31             }
32             return new Tuple<string, int>(description, order);
33 
34         }
35 
36         /// <summary>
37         /// 获取当前枚举的所有描述
38         /// </summary>
39         /// <returns></returns>
40         public static List<KeyValuePair<int, string>> GetAll<T>()
41         {
42             return GetAll(typeof(T));
43         }
44 
45         /// <summary>
46         /// 获取所有的枚举描述和值
47         /// </summary>
48         /// <param name="type"></param>
49         /// <returns></returns>
50         public static List<KeyValuePair<int, string>> GetAll(Type type)
51         {
52 
53             List<EnumToolsModel> list = new List<EnumToolsModel>();
54 
55             // 循环枚举获取所有的Fields
56             foreach (var field in type.GetFields())
57             {
58                 // 如果是枚举类型
59                 if (field.FieldType.IsEnum)
60                 {
61                     object tmp = field.GetValue(null);
62                     Enum enumValue = (Enum)tmp;
63                     int intValue = Convert.ToInt32(enumValue);
64                     var dec = enumValue.GetDescription();
65                     int order = dec.Item2;
66                     string showName = dec.Item1; // 获取描述和排序
67                     list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order });
68                 }
69             }
70 
71             // 排序并转成KeyValue返回
72             return list.OrderBy(i => i.Order).Select(i => new KeyValuePair<int, string>(i.Key, i.Name)).ToList();
73 
74         }
75         /// <summary>
76         /// 枚举Model
77         /// </summary> 
78         partial class EnumToolsModel
79         {
80             public int Order { get; set; }
81             public string Name { get; set; }
82             public int Key { get; set; }
83         }
84 
85     }

把原文中的out参数替换成返回元组,由于项目是vs2015开发,不能用c#7.0特性,否则用7.0中的值元组应该更好一点。性能和显示友好性都会有改进。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-06-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档