Craig Gibbons' Lifeblog lifeblog://


Creating a DropDownList from an Enum using Attributes and Reflection

Often in software development, one has a need to map the values in a lookup table to an enumeration in code. In this way, the values of the lookup table can be readily accessed in a strongly typed way. The only drawback to this approach is that the enum and the table can often get out of sync if values in the table change. That is however an entirely different problem and will not be discussed here. It is also often the case, in web development anyway, that one needs to display the values in the table and hence the enumeration, in a DropDowList. There are two possibly approaches to this problem.

  1. Get the values from the database in a IDataReader (or DataTable or whatever) and bind it to the DropDownList
  2. Use the values in the enumeration to generate the ListItems for the DropDownList.

I have always favoured the second approach, but enums are quite limited in the data they can hold. Really, it's nothing more than a stringly typed key/value pair, usually of type int. Sometimes however, we want to store more information with the enumeration for display in the UI. We may for example, have an enum with the following values:

public enum Vendors {

But, we may like to display something like "Microsoft Developmer Network" in a Drop Down. Enter Attributes! An Attribute can be applied to just about anything and can be easily consumed using Reflection. All the developer need do is create a class derived from the Attribute base class. It doesn't have to be particularly complex. The following will suffice:

public class EnumMetaDataAttribute : Attribute {
string _description;

public string Description {
get {
return _description;

public EnumMetaDataAttribute(string description) {
_description = description;

Then the attribute needs to be applied to the enum values:

public enum Vendors {
[EnumMetaData("Microsoft Developer Network")] Microsoft,
[EnumMetaData("Oracle Technology Network")] Oracle,
[EnumMetaData("IBM developerWorks")] IBM

So far, we have no way of consuming these attributes, but this is easily overcome using some basic reflection techniques. The following function should be fairly self explanatory. The .NET help should be sufficient reference for the reflection properties and methods:

public static void GetEnumDropDown(DropDownList dropDown, System.Type enumType) {
FieldInfo[] fields = enumType.GetFields();
foreach(FieldInfo field in fields) {
if(field.IsSpecialName) {

int value = Convert.ToInt32(field.GetValue(0));
EnumMetaDataAttribute attribute = field.GetCustomAttributes(typeof(EnumMetaDataAttribute), false)[0] as EnumMetaDataAttribute;

dropDown.Items.Add(new ListItem(attribute.Description, value.ToString()));

Which may then be called in the following way:

GetEnumDropDown(ddlDropDownName, typeof(Vendors));

Filed under: Tech Leave a comment
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.