利用自定义属性,定义枚举值的详细文本
新方案:http://hilite.cnblogs.com/archive/2006/03/28/360793.html
对于枚举类型:
很多情况下,要显示更完整或其他内容的枚举值文本,
解决方案之一,可以在某个地方维护一张对应表,来维护所有枚举值文本,这可能会导致枚举类型和文本脱节。
另一个方案是通过利用自定义属性“EnumDescription”,来定义枚举类型,这样枚举值和文本在一起,维护起来将很方便。
现在在调用代码:
这里是“EnumDescription”的具体实现和测试代码。http://files.cnblogs.com/hilite/EnumDisplayText.zip
这里是主要代码:

主要代码
1
/**//// <summary>
2
/// 得到枚举类型定义的所有文本
3
/// </summary>
4
/// <exception cref="NotSupportedException"></exception>
5
/// <param name="enumType">枚举类型</param>
6
/// <param name="sortType">指定排序类型</param>
7
/// <returns>所有定义的文本</returns>
8
public static EnumDescription[] GetTexts( Type enumType, SortType sortType )
9
{
10
EnumDescription[] descriptions = null;
11
//缓存处理
12
if ( cachedEnum.Contains(enumType.FullName) == false )
13
cachedEnum.Add(enumType.FullName, enumType.GetCustomAttributes(typeof(EnumDescription), false));
14
descriptions = (EnumDescription[])cachedEnum[enumType.FullName];
15
if ( descriptions.Length <= 0 ) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");
16
17
//按指定的属性冒泡排序
18
for ( int m = 0; m < descriptions.Length; m++ )
19
{
20
for ( int n = m; n < descriptions.Length; n++ )
21
{
22
EnumDescription temp;
23
bool swap = false;
24
25
switch ( sortType )
26
{
27
case SortType.Value:
28
if ( descriptions[m].EnumValue > descriptions[n].EnumValue ) swap = true;
29
break;
30
case SortType.DisplayText:
31
if ( string.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > 0 ) swap = true;
32
break;
33
case SortType.Rank:
34
if ( descriptions[m].EnumRank > descriptions[n].EnumRank ) swap = true;
35
break;
36
}
37
38
if ( swap )
39
{
40
temp = descriptions[m];
41
descriptions[m] = descriptions[n];
42
descriptions[n] = temp;
43
}
44
}
45
}
46
47
return descriptions;
48
}
对于枚举类型:
enum WorkState
{
/// <summary>
/// 计划
/// </summary>
Planing,
/// <summary>
/// 就绪
/// </summary>
Ready,
/// <summary>
/// 进行中
/// </summary>
Processing,
/// <summary>
/// 完成
/// </summary>
Finished
}
你的客户想怎么在界面上看到这些枚举类型的值呢?{
/// <summary>
/// 计划
/// </summary>
Planing,
/// <summary>
/// 就绪
/// </summary>
Ready,
/// <summary>
/// 进行中
/// </summary>
Processing,
/// <summary>
/// 完成
/// </summary>
Finished
}
MessageBox.Show(WorkState.Ready.ToString());
结果你一定想到了,是“Ready”,而不是“就绪”。很多情况下,要显示更完整或其他内容的枚举值文本,
解决方案之一,可以在某个地方维护一张对应表,来维护所有枚举值文本,这可能会导致枚举类型和文本脱节。
另一个方案是通过利用自定义属性“EnumDescription”,来定义枚举类型,这样枚举值和文本在一起,维护起来将很方便。
[EnumDescription(WorkState.Planing, "计划制定中")]
[EnumDescription(WorkState.Ready, "一切就绪")]
[EnumDescription(WorkState.Processing, "工作进行中")]
[EnumDescription(WorkState.Finished, "完成工作")]
enum WorkState
{
Planing,
Ready,
Processing,
Finished
}
[EnumDescription(WorkState.Ready, "一切就绪")]
[EnumDescription(WorkState.Processing, "工作进行中")]
[EnumDescription(WorkState.Finished, "完成工作")]
enum WorkState
{
Planing,
Ready,
Processing,
Finished
}
现在在调用代码:
MessageBox.Show(
EnumDescription.GetText(typeof(WorkState), (int)WorkState.Planing));
你将看到想要的文字“计划制定中”。EnumDescription.GetText(typeof(WorkState), (int)WorkState.Planing));
这里是“EnumDescription”的具体实现和测试代码。http://files.cnblogs.com/hilite/EnumDisplayText.zip
这里是主要代码:
1

/**//// <summary>2
/// 得到枚举类型定义的所有文本3
/// </summary>4
/// <exception cref="NotSupportedException"></exception>5
/// <param name="enumType">枚举类型</param>6
/// <param name="sortType">指定排序类型</param>7
/// <returns>所有定义的文本</returns>8
public static EnumDescription[] GetTexts( Type enumType, SortType sortType )9

{10
EnumDescription[] descriptions = null;11
//缓存处理12
if ( cachedEnum.Contains(enumType.FullName) == false )13
cachedEnum.Add(enumType.FullName, enumType.GetCustomAttributes(typeof(EnumDescription), false));14
descriptions = (EnumDescription[])cachedEnum[enumType.FullName];15
if ( descriptions.Length <= 0 ) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");16

17
//按指定的属性冒泡排序18
for ( int m = 0; m < descriptions.Length; m++ )19

{20
for ( int n = m; n < descriptions.Length; n++ )21

{22
EnumDescription temp;23
bool swap = false;24

25
switch ( sortType )26

{27
case SortType.Value:28
if ( descriptions[m].EnumValue > descriptions[n].EnumValue ) swap = true;29
break;30
case SortType.DisplayText:31
if ( string.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > 0 ) swap = true;32
break;33
case SortType.Rank:34
if ( descriptions[m].EnumRank > descriptions[n].EnumRank ) swap = true;35
break;36
}37

38
if ( swap )39

{40
temp = descriptions[m];41
descriptions[m] = descriptions[n];42
descriptions[n] = temp;43
}44
}45
}46

47
return descriptions;48
}