469,275 Members | 1,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

How to get enumeration type

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
Nov 21 '05 #1
7 8278

"Don" <un*****@oblivion.com> wrote in message
news:TBWad.86610$a41.31746@pd7tw2no...
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.
Nov 21 '05 #2
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.
"Imran Koradia" <no****@microsoft.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...

"Don" <un*****@oblivion.com> wrote in message
news:TBWad.86610$a41.31746@pd7tw2no...
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.

Nov 21 '05 #3
"Don" <un*****@oblivion.com> schrieb:
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.
Dim enumType As Type = objEnum.GetType()


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #4
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.

"Don" <un*****@oblivion.com> wrote in message
news:ybXad.87181$a41.9859@pd7tw2no...
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.
"Imran Koradia" <no****@microsoft.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl...

"Don" <un*****@oblivion.com> wrote in message
news:TBWad.86610$a41.31746@pd7tw2no...
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.


Nov 21 '05 #5
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
"Imran Koradia" <no****@microsoft.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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.

Nov 21 '05 #6

"Don" <un*****@oblivion.com> wrote in message
news:vdYad.88672$a41.40172@pd7tw2no...
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.
Nov 21 '05 #7
Don
"Imran Koradia" <no****@microsoft.com> wrote in message

news:<#n**************@tk2msftngp13.phx.gbl>...
"Don" <un*****@oblivion.com> wrote in message news:vdYad.88672$a41.40172@pd7tw2no...
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?


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
Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Justin Wright | last post: by
8 posts views Thread by aevans1108 | last post: by
1 post views Thread by Sergey Poberezovskiy | last post: by
3 posts views Thread by eXt | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.