.NET:您将使用什么属性来描述Enum值?

时间:2010-07-27 00:03:51

标签: .net

让我们说你有这个枚举:

Public Enum ApplicationStatus
    <Foo("Active")> Active = 1
    <Foo("Inactive")> Inactive = 2
    <Foo("Retired")> Retired = 3
    <Foo("In Development")> InDevelopment = 4
End Enum

对于Enum的纯文本描述,我应使用什么属性代替Foo?

3 个答案:

答案 0 :(得分:3)

我使用Description()

[Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }

答案 1 :(得分:1)

除了猎人的回答,你可能想看看this extension method来轻松找回描述

答案 2 :(得分:1)

我已使用Description for mine,但仅限于枚举本身需要以非常不同的方式显示的情况,否则它只显示枚举值的字符串。

我还使用一种方法检查枚举项中每个字符的大小写,如果它是1之后的大写字符,它会在字符前添加一个空格。

Public Enum ApplicationStatus
    Active = 1
    Inactive = 2
    Retired = 3
    InDevelopment = 4
    <Description("Radical Display Name")> Radical = 5 
End Enum
相关问题