Sign in to follow this  
Geronimo2000

[.net] Losing type information with enums

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this