.NET: What attribute would you use to describe an Enum value?
Lets say you have this enum:
Public Enum ApplicationSt开发者_运维百科atus
<Foo("Active")> Active = 1
<Foo("Inactive")> Inactive = 2
<Foo("Retired")> Retired = 3
<Foo("In Development")> InDevelopment = 4
End Enum
What attribute should I use in place of Foo for plain-text descriptions of the Enum?
I use Description()
[Description("The image associated with the control"),Category("Appearance")]
public Image MyImage {
get {
// Insert code here.
return image1;
}
set {
// Insert code here.
}
}
I've used Description for mine, but only in the cases when the enumeration itself needs to display very differently, otherwise it just displays the String of the Enum Value.
I've also use a method for some where it checks the case of each character in the Enum item and if it's an uppercase character after the 1st, it adds a space before the character.
Public Enum ApplicationStatus
Active = 1
Inactive = 2
Retired = 3
InDevelopment = 4
<Description("Radical Display Name")> Radical = 5
End Enum
In addition to hunter's answer, you might want to check out this extension method to retrieve the description easily
精彩评论