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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With