Question

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

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.

 45  44263  45
1 Jan 1970

Solution

 61

Use Reflection from the Type of the current class.

 Type superClass = myClass.GetType().BaseType;
2009-07-09

Solution

 23
Type superClass = typeof(MyClass).BaseType;

Additionally, if you don't know the type of your current object, you can get the type using GetType and then get the BaseType of that type:

Type baseClass = myObject.GetType().BaseType;

documentation

2009-07-09