Enum Display Text

Sometimes you need to display an enum value to the user. Most of the times you don’t want to display the name of the enum nor it’s numeric value but rather some nice description. Here is how you can do it using GetDisplayText extension method.

using OpenWaves;

...

enum MyEnum
{
    [DisplayText("This is value one")]
    Value1,
    Value2
}

...

MyEnum.Value1.GetDisplayText(); // Returns "This is value one"
MyEnum.Value2.GetDisplayText(); // Returns "Value2"

DisplayTextAttribute implements IDisplayTextProvider interface that is used by the extension method to resolve the text.

public interface IDisplayTextProvider
{
    string DisplayText { get; }
}

Last edited Aug 23, 2011 at 11:18 AM by mgrzyb, version 2

Comments

No comments yet.