To get a type's immediate parent, you can use the Type.BaseType
property. You can iteratively call BaseType
until it returns null
to walk up a type's inheritance hierarchy.
For example:
public static IEnumerable<Type> GetInheritanceHierarchy
(this Type type)
{
for (var current = type; current != null; current = current.BaseType)
yield return current;
}
Do note that it isn't valid to use System.Object
as the end-point because not all types (for example, interface types) inherit from it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…