Enum type from one of its members

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
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.
 
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.
 
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


Back
Top