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.
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.
Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.
Yes! Enums are also classes. We know that enums are static, and classes are not. Technically, you could replace Enum with a class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With