对于枚举类型:
enum WorkState
{
/// <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
}
现在在调用代码:
MessageBox.Show(
EnumDescription.GetText(typeof(WorkState), (int)WorkState.Planing));你将看到想要的文字“计划制定中”。这里是“EnumDescription”的具体实现和测试代码。http://www.cnblogs.com/Files/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
}你的客户想怎么在界面上看到这些枚举类型的值呢?
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
}
现在在调用代码:
MessageBox.Show(
EnumDescription.GetText(typeof(WorkState), (int)WorkState.Planing));你将看到想要的文字“计划制定中”。这里是“EnumDescription”的具体实现和测试代码。http://www.cnblogs.com/Files/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 }
解决方案 »
- C# windows服务程序安装包如何制作?
- 怎么关联两个文件?
- 怎样取消datagridview的默认选中单元格,急啊急啊
- 求一程序:实现将js文件的代码原样显示在IE里
- web service在嵌入式设备中被调用,使用的wince模拟器实现,但是在模拟器中出现了网络连接问题,请高手解决以下
- vs2005连接sql2005
- 问题可能挺初级,还请各位多指教
- 甘特图控件 PlexityHide.GTP.NET 如何注册?
- new 操作符的疑问......,在线恭候......
- 高分求助:“ASP:Calendar“控件的使用,在线等待!
- url(http:// , https:// , ftp:// 或内部网路径\\LEO)的正则表达式怎么写?
- 13分全部压上:在C#中用DC绘图,可以吗?望大家不吝赐教 等待ing...
http://www.cnblogs.com/hilite/archive/2006/03/28/360793.html