It's common to populate a drop-down box with enum values or, similarly, to display an enum setting to the user. The problem is that enum values usually aren't very pretty to look at. (Memories of hoping QA wouldn't spot "OrderShipped" come to mind.) In a previous post by Joe Niland, Joe suggested that attributes be used to maintain the user-friendly version of enum values. What follows is a solution for doing just that.
What we need is a class that will satisfy the following unit-test:
[TestFixture]
public class EnumDescriptionTests
{
[Test]
public void CanRetrieveEnumTextAndDescription() {
// Simply uses the enum value itself as the text representation by default
Assert.That(
EnumDescription.TextRepresentation.GetTextRepresentationOf(OrderStatus.Delivered),
Is.EqualTo("Delivered"));
Assert.That(
EnumDescription.TextRepresentation.GetDescriptionOf(OrderStatus.Delivered),
Is.EqualTo(String.Empty));
Assert.That(
EnumDescription.TextRepresentation.GetTextRepresentationOf(OrderStatus.InventoryDepleted),
Is.EqualTo("Out of Stock"));
Assert.That(
EnumDescription.TextRepresentation.GetDescriptionOf(OrderStatus.InventoryDepleted),
Is.EqualTo(String.Empty));
Assert.That(
EnumDescription.TextRepresentation.GetTextRepresentationOf(OrderStatus.InventoryComing),
Is.EqualTo("More in Stock Soon"));
Assert.That(
EnumDescription.TextRepresentation.GetDescriptionOf(OrderStatus.InventoryComing),
Is.EqualTo("An order for more items has been placed with the supplier"));
}
private enum OrderStatus
{
Delivered,
[EnumDescription.TextRepresentation("Out of Stock")]
InventoryDepleted,
[EnumDescription.TextRepresentation("More in Stock Soon",
"An order for more items has been placed with the supplier")]
InventoryComing
}
}
When a description is provided for an enum value, via the "Description" attribute, then the provided description is returned. When a description isn't available, the ToString version of the enum value, with spaces put before each capital letter, is returned, instead.
So then to satisfy the unit test:
public class EnumDescription
{
public class TextRepresentationAttribute : Attribute
{
public readonly string Text;
public readonly string Description = "";
public TextRepresentationAttribute(string text) {
Text = text;
Description = "";
}
public TextRepresentationAttribute(string text, string description) {
Text = text;
Description = description;
}
}
public class TextRepresentation
{
public static string GetTextRepresentationOf(Enum enumType) {
TextRepresentationAttribute textRepresentation = GetTextRepresentationAttributeFor(enumType);
return textRepresentation != null
? textRepresentation.Text
: enumType.ToString();
}
public static string GetDescriptionOf(Enum enumType) {
TextRepresentationAttribute textRepresentation = GetTextRepresentationAttributeFor(enumType);
return textRepresentation != null
? textRepresentation.Description
: String.Empty;
}
private static TextRepresentationAttribute GetTextRepresentationAttributeFor(Enum enumType) {
MemberInfo[] memberInfo = enumType.GetType().GetMember(enumType.ToString());
if (memberInfo != null && memberInfo.Length == 1) {
object[] customAttributes =
memberInfo[0].GetCustomAttributes(typeof(TextRepresentationAttribute), false);
if (customAttributes.Length == 1) {
return (TextRepresentationAttribute) customAttributes[0];
}
}
return null;
}
}
}
This should make a useful addition to any utility library.
Billy
Posted
10-16-2006 2:39 PM
by
Billy McCafferty