Click here to Skip to main content
13,410,685 members (32,433 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 25 Apr 2003

Enums powered by Reflection

, 25 Apr 2003
Rate this:
Please Sign up or sign in to vote.
An article about building enums in runtime by means of System.Reflection.Emit
<!-- Article image -->

Sample Image - enumsReflection.jpg

<!-- Add the rest of your HTML here -->


C# enums are a quite powerful element of the C# language. Because of its metadata stored in the assembly they provide us with much more info than their equivalent in C++. What´s more, using System.Reflection.Emit we can delay the creation of an enum till runtime.

Using the code

In C# (as in C++) every instance of an enum type takes 4 bytes in memory (only the integer value associated to the string value is stored), you can use sizeof to verify this, so it´s a really storage efficient data structure. The great thing in C# is that for every enum class in our code the string values are stored in the assembly as metadata, so we can access to it in our code. We can use for it methods like Enum.GetNames(). Here´s a little sample:

enum Language
class App
    public static void Main()
        Console.WriteLine("Write the number of the selected Language");
        string[] langAr=Enum.GetNames(Type.GetType("Language"));
        for(int i=0;i<langAr.Length;i++)
            Console.WriteLine(i + "." + langAr[i]);
        Language myLang=(Language)Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Your Language of choice is: " + myLang);
Another great thing about C# enums is that we can create an enum class in runtime, after the users have fed our program with data. In the following sample we want an enum with the months that our business offers to our employees to choose as holidays month. The problem is that we don´t know till runtime which months we are going to offer to our employees. System.Reflection.Emit comes in our help. First we fill a string with the source code for the new enum (Holidays), then we write it to disk and compile it. Finally we load the new assembly. Since that moment we can work with the new enum we´ve coded, and we can create instances of that new enum. One useful method we need to assign values to the instances of our new enum class is Enum.parse Here it´s how it works:
Employee emp=new Employee();
Console.WriteLine("Insert Employee data");
string hm=Console.ReadLine();
//Create an instance of our dynamic enum (Holidays)
    //assign the right value to the enum instance
For this sample I make use of a small, incomplete and untested Library (ReflectiveTools) that I´ve coded to make easier some of the most common reflection operations (saving dynamic code to disk and compiling it). This library is included in the zip file. Well, I think you have to download the source code to really understand the purpose of this article.


I hope this article to be an example of how useful C# enums can be.

I really can´t understand why languages like Java have excluded enums of the language.</p

One last thing, some of the comments in the source code are written in Spanish, I´m sorry, but that´s my native language. Anyway I don´t think those comments are too important.



  • 04/04/2003: This article is submitted to CodeProject


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Spain Spain
My name is Jose Luis Sampayo Aller, I´m from Xixón-Asturies-Spain.
I´ve got a degree in Computer Science by University of Oviedo.
I´ve been having fun with .Net since April 2002.
My main current interests are: .Net and Mono, OOP (mainly C# and C++), IA-32 assembler (hope someday I´ll learn it), OS Internals (mainly Nt family). Outside computer world I´m really interested in Neuroscience, Science in general, Science Fiction...

You may also be interested in...

Comments and Discussions

Generalcan't read enum in other assembly using reflection Pin
Do.It5-Jul-07 22:50
memberDo.It5-Jul-07 22:50 
AnswerRe: can't read enum in other assembly using reflection Pin
LazyTarget15-May-13 8:37
memberLazyTarget15-May-13 8:37 
GeneralGood article but I have a question Pin
mrsnipey11-Sep-06 1:12
membermrsnipey11-Sep-06 1:12 
GeneralDynamic Enums without writing to file Pin
nlgarvey21-Jun-06 5:59
membernlgarvey21-Jun-06 5:59 
GeneralItem count Pin
AlainT21-Sep-05 5:33
memberAlainT21-Sep-05 5:33 
Generalsimple to understand but example could be better Pin
dog_spawn26-Apr-03 13:51
memberdog_spawn26-Apr-03 13:51 
GeneralRe: simple to understand but example could be better Pin
Lennin10-Nov-03 0:55
memberLennin10-Nov-03 0:55 
GeneralEnums can be 1, 2, 4, or 8 bytes long Pin
Wesner Moise26-Apr-03 12:07
memberWesner Moise26-Apr-03 12:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180221.1 | Last Updated 26 Apr 2003
Article Copyright 2003 by XoseLluis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid