Enum type from one of its members

P

Paul E Collins

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.
 
C

Christof Nordiek

Paul E Collins said:
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.
 
S

Stoitcho Goutsev \(100\)

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();
 

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

Similar Threads

Enum Extentions 7
number of values in enum 2
Enum To String 14
Display enum in ComboBox with spaces 7
enum basics 7
Passing enums to a method 5
Casting as an enum type 2
Deserialization of invalid enum values 3

Top