在我们写程序的时候,有时会使用一些枚举来表示现在的状态或者功能,这时我们可能要在界面上显示状态或功能,但是我们并不希望直接用枚举本身的符号名称,而是想使用其它的解释来表示,这时我们就可以使用特性来描述着解释了。
例如:
1
public enum EnumType
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
[EnumExplain("一")]
4
ONE,
5
[EnumExplain("二")]
6
TWO,
7
[EnumExplain("三")]
8
THREE,
9
} 那么我们应该怎么做呢?
首先,我们要定义一个特性,来告诉程序每个枚举成员的解释是什么。
代码为:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
2
public class EnumExplainAttribute : Attribute
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
4
public EnumExplainAttribute(string explain)
5
: base()
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
m_explain = explain;
8
}
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
private string m_explain;
11![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
public string Explain
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_explain; }
15
}
16
}
然后,就是我们如何获取枚举成员的解释了,这就是我们这次要做的
枚举解释器了。
代码为:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public class EnumExplainDictionary<T> where T : struct
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
public EnumExplainDictionary()
4
: base()
5![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
6
Init_enumExplainDictionary();
7
}
8![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
private void Init_enumExplainDictionary()
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
FieldInfo[] fieldInfos = typeof(T).GetFields();
12
EnumExplainAttribute explain;
13
for (int i = 1; i < fieldInfos.Length; i++)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
if (!Attribute.IsDefined(fieldInfos[i], typeof(ObsoleteAttribute)))
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
explain = Attribute.GetCustomAttribute(fieldInfos[i], typeof(EnumExplainAttribute)) as EnumExplainAttribute;
18
if (explain != null)
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
m_keyCollection.Add((T)fieldInfos[i].GetValue(null));
21
m_explainCollection.Add(explain.Explain);
22
}
23
}
24
}
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
private List<T> m_keyCollection = new List<T>();
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
public List<T> KeyCollection
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_keyCollection; }
32
}
33![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
private List<string> m_explainCollection = new List<string>();
35![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
public List<string> ExplainCollection
37![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return m_explainCollection; }
39
}
40![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
public T this[string explain]
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
get
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
return GetVaule(explain, m_explainCollection, m_keyCollection);
46
}
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
public string this[T key]
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
51
get
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
return GetVaule(key, m_keyCollection, m_explainCollection);
54
}
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
private U GetVaule<K, U>(K vaule, List<K> kCollection, List<U> uCollection)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
foreach (K item in kCollection)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
62
if (item.ToString() == vaule.ToString())
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
return uCollection[kCollection.IndexOf(item)];
65
}
66
}
67
if (typeof(U) == typeof(string))
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
throw new ExplainNotFoundException("该枚举成员没有找到对应得解释");
70
}
71
else
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
73
throw new KeyNotFoundException("该解释没有找到对应的枚举成员");
74
}
75
}
76
}
77
public class ExplainNotFoundException : Exception
78![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
79
public ExplainNotFoundException()
80
: base()
81![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
82
public ExplainNotFoundException(string message)
83
: base(message)
84![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
85
public ExplainNotFoundException(string message, Exception innerException)
86
: base(message, innerException)
87![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
88
}
最后,就是我们如何来使用这个
枚举解释器了。
在这里举个例子:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
class Program
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
static void Main(string[] args)
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
Console.WriteLine(item);
11
}
12
Console.Read();
13
}
14
}
结果:
三
TWO
一
二
三
源代码下载地址:
http://www.rayfile.com/zh-cn/files/442a8407-995e-11de-a6bd-0014221b798a/0194df2d/