Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create an enum [duplicate]

Tags:

c#

I have an enum of the following structure:

public enum DType {            LMS =  0,     DNP = -9,     TSP = -2,     ONM =  5,     DLS =  9,     NDS =  1 } 

I'm using this enum to get the names and the values. Since there is a requirement to add more types, I need to read the type and the values from an XML file. Is there any way by which I can create this enum dynamically from XML file so that I can retain the program structure.

like image 656
Milen Avatar asked May 13 '09 11:05

Milen


People also ask

Can we create a enum dynamically?

You can dynamically create source code by reading from the database and simply outputting the results in a format conducive to building an enum. However, it is impractical to create an enum at run time.

Can enum have duplicate values?

Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can we add enum dynamically in Java?

Yes! Enums are also classes. We know that enums are static, and classes are not. Technically, you could replace Enum with a class.


1 Answers

Probably, you should consider using a Dictionary<string, int> instead.

If you want to generate the enum at compile-time dynamically, you might want to consider T4.

like image 70
mmx Avatar answered Sep 20 '22 14:09

mmx