:::: MENU ::::

Tuesday, January 20, 2009

Wherever possible we use enumerations in our applications. Often the name of the enumeration isn't what you want to print on the screen. It could be because the enumeration is 2 words that have been joined together, or you want the description to start with a number (not that we do this... but you could).

Example

You might have a enumeration for Colours that a user is allowed to select from. The values available for this enumeration may be (hypothetically):

  • Burnt Orange
  • Bright Pink
  • Dark Green
  • Sky Blue

To define this in code, you'd use an enumeration like this:

public enum UserColours

{

    BurntOrange = 1,

    BrightPink = 2,

    DarkGreen = 3,

    SkyBlue = 4

}

The Problem

Normally, in an ASP.NET application we'd render an enumeration as a Drop Down List. Problem is we don't want to show "BurntOrange" as a value in the drop down list, we want to show a nice friendly option like "Burnt Orange".

The Solution

We've created a static helper class that uses reflection to get a Description for each member of the enumeration. I've also got this as an extension method for ASP.NET 3.5. This post is for the helper class, but it can very easily be converted to an extension method for ToString().

To get a friendly name, we decorate each member of the enumeration with the DescriptionAttribute from the namespace System.ComponentModel. Our enums end up looking like this:

using System.ComponentModel;

 

namespace Ellington.EnumHelperExamples

{

    public enum UserColours

    {

        [Description("Burnt Orange")]

        BurntOrange = 1,

 

        [Description("Bright Pink")]

        BrightPink = 2,

 

        [Description("Dark Green")]

        DarkGreen = 3,

 

        [Description("Sky Blue")]

        SkyBlue = 4

    }

}

When we need to then retrieve the friendly name from the enum, we have the following helper class:

using System;

using System.ComponentModel;

using System.Reflection;

 

namespace Ellington.EnumHelperExamples

{

    public static class EnumHelper

    {

        /// <summary>

        /// Retrieve the description on the enum, e.g.

        /// [Description("Bright Pink")]

        /// BrightPink = 2,

        /// Then when you pass in the enum, it will retrieve the description

        /// </summary>

        /// <param name="en">The Enumeration</param>

        /// <returns>A string representing the friendly name</returns>

        public static string GetDescription(Enum en)

        {

            Type type = en.GetType();

 

            MemberInfo[] memInfo = type.GetMember(en.ToString());

 

            if (memInfo != null && memInfo.Length > 0)

            {

                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

 

                if (attrs != null && attrs.Length > 0)
                {

                    return ((DescriptionAttribute)attrs[0]).Description;
                }

            }

 

            return en.ToString();

        }

    }

}

Now, when we want to get the friendly name from the enum, all we do is call:


EnumHelper.GetDescription(UserColours.BrightPink);

The code above, basically takes in the enumeration, uses Reflection to find the "DescriptionAttribute" on the member, and then returns the Description. If the DescriptionAttribute is not present on the enum, then we just call a ToString() on the enum to get the name.

As we're not hitting anything that is scary from a security point of view, the whole thing works in a partial trust environment.