locked
Dynamically create Enums from Lookup DB tables RRS feed

  • Question

  • Hi all,

    I have a bunch of Lookup tables in my database and would like to use these as Enums in my code (is this the best way of representing Lookup tables in code?)  and was wondering if there is any way of doing this (i.e. like dynamically create Enums at runtime?) Or must Enums be compiled at design time?
     
    Thanks
    Sunday, January 6, 2008 8:41 PM

Answers

  • Actually you can dynamically create enums at runtime, either by using Reflection.Emit or CodeDOM.

    Of course, the problem is that if they don't exist until runtime, you can't actually use them or any of their values in your code because you haven't created them yet. So as suggested above you're probably better off using a list or a dictionary to represent the values.
    Monday, January 7, 2008 8:42 AM

All replies

  • No, that's certainly not possible.
    What do you think of storing the lookup tables in a Dictionary(as name-value pairs) or in a NameValueList?
    Sunday, January 6, 2008 9:08 PM
  • I dont mind sticking them in a Dictionary, but I need a object to have an attribute set much like having it set an a value/field of an enum.

    Thanks
    Monday, January 7, 2008 12:25 AM
  • Actually you can dynamically create enums at runtime, either by using Reflection.Emit or CodeDOM.

    Of course, the problem is that if they don't exist until runtime, you can't actually use them or any of their values in your code because you haven't created them yet. So as suggested above you're probably better off using a list or a dictionary to represent the values.
    Monday, January 7, 2008 8:42 AM