How to get enumeration type

D

Don

I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

....

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum


Is there any way to do this?

- Don
 
I

Imran Koradia

Don said:
I need to get the type of an enumeration from an instance of a class.

e.g.

Public Class MyClass
Public Enum MyEnum
value1 = 1
End Enum
End Class

...

Dim obj as New MyClass
Dim enumType as Type

enumType = the type of obj.MyEnum


Is there any way to do this?

Dim objEnum As MyClass.MyEnum
Dim enumType As Type = objEnum.GetType()


hope that helps..
Imran.
 
D

Don

Maybe I should have been more clear: I have multiple classes with an
enumeration of the same name in each, but each enumeration is slightly
different. I don't know what type the object will be at runtime, so I can't
do MyClass.MyEnum. I need to be able to get the type of the object, then
get the type of the enum in that class.
 
H

Herfried K. Wagner [MVP]

Don said:
Maybe I should have been more clear: I have multiple
classes with an enumeration of the same name in each,
but each enumeration is slightly different. I don't know
what type the object will be at runtime, so I can't
do MyClass.MyEnum. I need to be able to get the
type of the object, then get the type of the enum in that class.

'GetType' on the object holding the enum value will do what you want, as far
as I understand. But that's what Imran already suggested.
 
I

Imran Koradia

Dim obj As New MyClass
Dim enumType = obj.GetType().GetNestedType("MyEnum")
Messagebox.Show(enumType.Name)

Note that the search is case-sensitive; So "myEnum" instead of "MyEnum" will
return nothing.


hope that helps..
Imran.
 
D

Don

That's exactly what I need. It only seems to work if the enumeration is
public, though.

Also, what is returned doesn't appear to be accepted by the
System.Enum.GetName(), method. It's like it doesn't recognize it as an
enumeration if I get the enumeration type in this manner. Any thoughts?

- Don
 
I

Imran Koradia

Don said:
That's exactly what I need. It only seems to work if the enumeration is
public, though.

To get the both public and private enums, use the other overloaded version
of GetNestedType:

Dim obj As New MyClass
Dim enumType = obj.GetType().GetNestedType("MyEnum", _
Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
Messagebox.Show(enumType.Name)
Also, what is returned doesn't appear to be accepted by the
System.Enum.GetName(), method. It's like it doesn't recognize it as an
enumeration if I get the enumeration type in this manner. Any thoughts?

This worked for me:
MessageBox.Show(System.Enum.GetName(enumType, 1))

Is that not what you are looking for?

hope that helps..
Imran.
 
D

Don

To get the both public and private enums, use the other overloaded version
of GetNestedType:

Dim obj As New MyClass
Dim enumType = obj.GetType().GetNestedType("MyEnum", _
Reflection.BindingFlags.Public Or
Reflection.BindingFlags.NonPublic)




This worked for me:
MessageBox.Show(System.Enum.GetName(enumType, 1))

Is that not what you are looking for?

Yes, this is exactly what I'm looking for and I'm doing pretty much same

thing you are (obj is passed as a parameter and not instantiated in the

function, as in your example, though), but the call to Enum.GetName raises

the following error: "Type provided must be an Enum." When I use

obj.GetType().GetNestedType() to return an Enum's Type and store it into a

"type" variable, it doesn't seem to remember that it really is an enum.

Thanks for your help. This is exactly what I need. I just wish it would

work properly. :(

- Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top