DescriptionAttribute
By applying the [Description]
attribute to an enum member, Enum Utilities provides convenient methods for retrieving and parsing descriptions.
Example
csharp
using System.ComponentModel;
[EnumGenerator]
public enum PaymentMethod
{
[Description("The payment by using physical cash")]
Cash,
Cheque
}
This will generate code on the following classes:
Additional Features
GetDescription()
- A generated extension method in
<EnumName>Extensions
that reads the text specified by[Description("...")]
. - If
[Description]
is omitted on a member, the generator returnsnull
.
- A generated extension method in
TryCreateFromDescription()
- A method in
<EnumName>Factory
that attempts to parse a string back into the enum based on the correspondingDescriptionAttribute
. - Example:csharp
bool success = PaymentMethodFactory.TryCreateFromDescription( "The payment by using physical cash", out var method ); // success == true // method == PaymentMethod.Cash
- A method in
When to Use
Use DescriptionAttribute
when you want a user-friendly label or explanation for an enum member, often displayed in UIs or printed in logs. This attribute is straightforward to apply and makes your enum values more descriptive for end users or developers.