By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,334 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,334 IT Pros & Developers. It's quick & easy.

Enum type from one of its members

P: n/a
How can I get the type of an enum given one of its members?

object memb = MyEnum.SomeValue;
Type parent = SOMETHING(memb);

Afterwards, the 'parent' variable should hold typeof(MyEnum).

Eq.
Jan 29 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Paul E Collins" <fi******************@CL4.orgschrieb im Newsbeitrag
news:zN******************************@bt.com...
How can I get the type of an enum given one of its members?

object memb = MyEnum.SomeValue;
Type parent = SOMETHING(memb);
Type parent = memb.GetType();

That should do it.
>
Afterwards, the 'parent' variable should hold typeof(MyEnum).

Eq.


Jan 29 '07 #2

P: n/a
Paul,

There is a method GetType declared on the level of the base Object type.
That means it can be called for any reference regardless of its type. When
this method is called it returns the actual runtime class of the object.
This method cannot be overloaded, so it always return the correct type and
cannot be fooled.

Type t = memb.GetType();
--
HTH
Stoitcho Goutsev (100)

"Paul E Collins" <fi******************@CL4.orgwrote in message
news:zN******************************@bt.com...
How can I get the type of an enum given one of its members?

object memb = MyEnum.SomeValue;
Type parent = SOMETHING(memb);

Afterwards, the 'parent' variable should hold typeof(MyEnum).

Eq.


Jan 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.