C# : how do you obtain a class' base class?

JaysonFix picture JaysonFix · Jul 9, 2009 · Viewed 36k times · Source

In C#, how does one obtain a reference to the base class of a given class?

For example, suppose you have a certain class, MyClass, and you want to obtain a reference to MyClass' superclass.

I have in mind something like this:

Type  superClass = MyClass.GetBase() ;
// then, do something with superClass

However, it appears there is no suitable GetBase method.

Answer

JoshJordan picture JoshJordan · Jul 9, 2009

Use Reflection from the Type of the current class.

 Type superClass = myClass.GetType().BaseType;