利用自定义属性,定义枚举值的详细文本

本文介绍了一种使用自定义属性'EnumDescription'的方法,该方法能够将枚举值与其描述性文本相绑定,方便在界面上显示更加友好的枚举值文本。通过具体的实现代码示例,展示了如何获取和展示枚举类型的描述性文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新方案: http://hilite.cnblogs.com/archive/2006/03/28/360793.html

对于枚举类型:
     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://files.cnblogs.com/hilite/EnumDisplayText.zip

这里是主要代码:
ContractedBlock.gif ExpandedBlockStart.gif 主要代码
 1ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
 2InBlock.gif        /// 得到枚举类型定义的所有文本
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <exception cref="NotSupportedException"></exception>
 5InBlock.gif        /// <param name="enumType">枚举类型</param>
 6InBlock.gif        /// <param name="sortType">指定排序类型</param>
 7ExpandedBlockEnd.gif        /// <returns>所有定义的文本</returns>

 8None.gif        public static EnumDescription[] GetTexts( Type enumType, SortType sortType )
 9ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
10InBlock.gif            EnumDescription[] descriptions = null;
11InBlock.gif            //缓存处理
12InBlock.gif            if ( cachedEnum.Contains(enumType.FullName) == false )
13InBlock.gif                cachedEnum.Add(enumType.FullName, enumType.GetCustomAttributes(typeof(EnumDescription), false));
14InBlock.gif            descriptions = (EnumDescription[])cachedEnum[enumType.FullName];
15InBlock.gif            if ( descriptions.Length <= 0 ) throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");
16InBlock.gif
17InBlock.gif            //按指定的属性冒泡排序
18InBlock.gif            for ( int m = 0; m < descriptions.Length; m++ )
19ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
20InBlock.gif                for ( int n = m; n < descriptions.Length; n++ )
21ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
22InBlock.gif                    EnumDescription temp;
23InBlock.gif                    bool swap = false;
24InBlock.gif
25InBlock.gif                    switch ( sortType )
26ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
27InBlock.gif                        case SortType.Value:
28InBlock.gif                            if ( descriptions[m].EnumValue > descriptions[n].EnumValue ) swap = true;
29InBlock.gif                            break;
30InBlock.gif                        case SortType.DisplayText:
31InBlock.gif                            if ( string.Compare(descriptions[m].EnumDisplayText, descriptions[n].EnumDisplayText) > 0 ) swap = true;
32InBlock.gif                            break;
33InBlock.gif                        case SortType.Rank:
34InBlock.gif                            if ( descriptions[m].EnumRank > descriptions[n].EnumRank ) swap = true;
35InBlock.gif                            break;
36ExpandedSubBlockEnd.gif                    }

37InBlock.gif
38InBlock.gif                    if ( swap )
39ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
40InBlock.gif                        temp = descriptions[m];
41InBlock.gif                        descriptions[m] = descriptions[n];
42InBlock.gif                        descriptions[n] = temp;
43ExpandedSubBlockEnd.gif                    }

44ExpandedSubBlockEnd.gif                }

45ExpandedSubBlockEnd.gif            }

46InBlock.gif
47InBlock.gif            return descriptions;
48ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/hilite/archive/2006/03/25/358420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值