Is it possible in C#, via reflection or some other method, to return a list all superclasses (concrete and abstract, mostly interested in concrete classes) of an object. For example passing in a "Tiger" class would return:
- Tiger
- Cat
- Animal
- Object
Is it possible in C#, via reflection or some other method, to return a list all superclasses (concrete and abstract, mostly interested in concrete classes) of an object. For example passing in a "Tiger" class would return:
static void VisitTypeHierarchy(Type type, Action<Type> action) {
if (type == null) return;
action(type);
VisitTypeHierarchy(type.BaseType, action);
}
Example:
VisitTypeHierarchy(typeof(MyType), t => Console.WriteLine(t.Name));
You can easily deal with abstract classes by using the Type.IsAbstract
property.