• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
flashpast
博客园    首页    新随笔    联系   管理    订阅  订阅

枚举解释器

在我们写程序的时候,有时会使用一些枚举来表示现在的状态或者功能,这时我们可能要在界面上显示状态或功能,但是我们并不希望直接用枚举本身的符号名称,而是想使用其它的解释来表示,这时我们就可以使用特性来描述着解释了。
例如:
1    public enum EnumType
2    {
3        [EnumExplain("一")]
4        ONE,
5        [EnumExplain("二")]
6        TWO,
7        [EnumExplain("三")]
8        THREE,
9    }

 那么我们应该怎么做呢?
首先,我们要定义一个特性,来告诉程序每个枚举成员的解释是什么。
代码为:
Code
 1    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
 2    public class EnumExplainAttribute : Attribute
 3    {
 4        public EnumExplainAttribute(string explain)
 5            : base()
 6        {
 7            m_explain = explain;
 8        }

 9
10        private string m_explain;
11
12        public string Explain
13        {
14            get { return m_explain; }
15        }

16    }

然后,就是我们如何获取枚举成员的解释了,这就是我们这次要做的枚举解释器了。
代码为:
Code
 1    public class EnumExplainDictionary<T> where T : struct
 2    {
 3        public EnumExplainDictionary()
 4            : base()
 5        {
 6            Init_enumExplainDictionary();
 7        }

 8
 9        private void Init_enumExplainDictionary()
10        {
11            FieldInfo[] fieldInfos = typeof(T).GetFields();
12            EnumExplainAttribute explain;
13            for (int i = 1; i < fieldInfos.Length; i++)
14            {
15                if (!Attribute.IsDefined(fieldInfos[i], typeof(ObsoleteAttribute)))
16                {
17                    explain = Attribute.GetCustomAttribute(fieldInfos[i], typeof(EnumExplainAttribute)) as EnumExplainAttribute;
18                    if (explain != null)
19                    {
20                        m_keyCollection.Add((T)fieldInfos[i].GetValue(null));
21                        m_explainCollection.Add(explain.Explain);
22                    }

23                }

24            }

25        }

26
27        private List<T> m_keyCollection = new List<T>();
28
29        public List<T> KeyCollection
30        {
31            get { return m_keyCollection; }
32        }

33
34        private List<string> m_explainCollection = new List<string>();
35
36        public List<string> ExplainCollection
37        {
38            get { return m_explainCollection; }
39        }

40
41        public T this[string explain]
42        {
43            get
44            {
45                return GetVaule(explain, m_explainCollection, m_keyCollection);
46            }

47        }

48
49        public string this[T key]
50        {
51            get
52            {
53                return GetVaule(key, m_keyCollection, m_explainCollection);
54            }

55        }

56
57
58        private U GetVaule<K, U>(K vaule, List<K> kCollection, List<U> uCollection)
59        {
60            foreach (K item in kCollection)
61            {
62                if (item.ToString() == vaule.ToString())
63                {
64                    return uCollection[kCollection.IndexOf(item)];
65                }

66            }

67            if (typeof(U) == typeof(string))
68            {
69                throw new ExplainNotFoundException("该枚举成员没有找到对应得解释");
70            }

71            else
72            {
73                throw new KeyNotFoundException("该解释没有找到对应的枚举成员");
74            }

75        }

76    }

77    public class ExplainNotFoundException : Exception
78    {
79        public ExplainNotFoundException()
80            : base()
81        { }
82        public ExplainNotFoundException(string message)
83            : base(message)
84        { }
85        public ExplainNotFoundException(string message, Exception innerException)
86            : base(message, innerException)
87        { }
88    }

最后,就是我们如何来使用这个枚举解释器了。
在这里举个例子:
Code
 1    class Program
 2    {
 3        static void Main(string[] args)
 4        {
 5            EnumExplainDictionary<EnumType> dictionary = new EnumExplainDictionary<EnumType>();
 6            Console.WriteLine(dictionary[EnumType.THREE]);
 7            Console.WriteLine(dictionary["二"]);
 8            foreach (string item in dictionary.ExplainCollection)
 9            {
10                Console.WriteLine(item);
11            }

12            Console.Read();
13        }

14    }

结果:
三
TWO
一
二
三

源代码下载地址:http://www.rayfile.com/zh-cn/files/442a8407-995e-11de-a6bd-0014221b798a/0194df2d/
posted @ 2009-09-05 15:33  flashpast  阅读(330)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3