[.net] Losing type information with enums

Started by
4 comments, last by RenZimE 19 years, 7 months ago
I desperately need variables that hold enum values to retain their type information like every other variable in my program. Whenever I have to upcast to an object, the reported type is System.Object with no underlying types, and if I query for the type using GetType(), I see that the type is "Class + Enum". The plus sign is weird in the syntax. Any suggestions?
Advertisement
This is because an enum is simply an integer. It's not a specific data type in the same sense as a struct or class. While working on the compiler, I also discovered it doesn't even retain access specifiers (a private enum in an assembly can be imported and used in another assembly, the framework won't stop you).

Matt
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
Why do you need to know the type of the enum? (Basically the name of the enum class).
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.
Yeah, an enum is only half a type ... there is a type entry for it, but when being used, you are using instances of type "Enum" and there is a sub-type that is their type.

In fact, to convert from a string to an enum value, you do not do:

MyEnumType.Parse(stringValue)

you do:

Enum.Parse(typeof(MyEnumType), stringValue)

in fact if you look closely you will see that the enum has primarily static functions NOT virtual functions ... so it isn't a base class for other classes, but instead of utility class itself.
Oh, I see. I managed to work around the problem this time, but it was an interesting excercise none the less. All the advice in this thread was very helpful and straightforward - I appreciate the help.

you could make your enum inherit a type, i do this a lot to ease serialization,

enum bleh : byte
{
bleh_zero=0,
bleh_one
}

This topic is closed to new replies.

Advertisement