posts - 234, comments - 480, trackbacks - 56

My Links

News




I am born in Bangladesh and currently live in Melbourne, Australia. I am a co-founder and core developer of Pageflakes www.pageflakes.com and CEO at Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.

I also created SmartCodeGenerator

Some of my articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

How to Get Enum Values with Reflection in C#

This came up when I was writing a UIProperty for SmartCodeGenerator,
The combo box should display all the available enum options in a DropDownList for the enduser to choose from them.

So if someone defines a enum like this:
public enum MyEnum
{
  ten = 10,
  twenty = 20 ,
  thirty = 30
}


and a class using the enum as one of its property, ie.

public class TheProperties
{
  public TheProperties( )
  {
    //Please Assign Default Values here
    this.myVar = MyEnum.twenty;
  }

  private MyEnum testEnumProp; 

  public MyEnum TestEnumProp
  {
    get { return testEnumProp; }
    set { testEnumProp = value; }
  }

}


For this declaration above my Objective is to comeup with a ASP.Net DropDownList  with the endresult in html like this:

<select name="MyVar$ddProperty" id="MyVar_ddProperty">
  <option value="10">ten</option>
  <option selected="selected" value="20">twenty</option>
  <option value="30">thirty</option>
</select>

Ok here is the code to achieve this:

object profile = new TheProperties();
foreach (PropertyInfo propertyInfo in profile.GetType().GetProperties())
{
  if ((info.PropertyType.IsEnum) && (info.PropertyType.IsPublic))
  {
    foreach (FieldInfo fInfo in this.propertyInfo.PropertyType.GetFields(BindingFlags.Public | BindingFlags.Static))
    {
      //ListItem item = new ListItem(fInfo.Name, ((int)fInfo.GetValue(this.propertyInfo)).ToString());//.Net1.1
      ListItem item = new ListItem(fInfo.Name, fInfo.GetRawConstantValue().ToString());
      ddProperty.Items.Add(item);
    }
  }
}

in .Net2.0 we have this nice little function GetRawConstantValue() which returns the value associated with enum (in this example 10, 20, 30)
in .Net1.1 we have to do it in old fashioned way with the GetValue(...) method.

Print | posted on Wednesday, December 06, 2006 12:42 AM |

Feedback

Gravatar

# re: How to Get Enum Values with Reflection in C#

Shouldn't the variable info be propertyInfo?
7/7/2007 1:32 AM | Kris Wenzel
Gravatar

# re: How to Get Enum Values with Reflection in C#

MemberInfo[] memberInfos = typeof(MyEnum).GetMembers(BindingFlags.Public | BindingFlags.Static);
string alerta = "";
for (int i = 0; i < memberInfos.Length; i++) {
alerta += memberInfos[i].Name + " - ";
alerta += memberInfos[i].GetType().Name + "\n";
}
MessageBox.Show(alerta);
7/15/2008 6:37 PM | Jose Americo Antoine
Gravatar

# re: How to Get Enum Values with Reflection in C#

dim value as Integer = Ctype(MyEnum,Integer)
4/2/2009 4:14 AM | dELM
Gravatar

# re: How to Get Enum Values with Reflection in C#

Why do you do that in so complex way?
To do the same using ready .Net possibilities (VB .NET syntax)

dim names = System.Enum.GetNames(GetType(MyEnum))
dim values = System.Enum.GetValues(GetType(MyEnum))
for i as integer = 0 To names.Length - 1
dim item = new ListItem(names(i), values(i));
ddProperty.Items.Add(item);
next
6/30/2009 9:00 PM | cheburek
Gravatar

# re: How to Get Enum Values with Reflection in C#

I get enum value without Reflection in C#:
MyEnum me = MyEnum.twenty;
int iValue = (int) me;
... iValue should result 20? ...
8/6/2009 8:06 PM | dK
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: