我希望能够找到特定类型的所有父类型(基类和接口)。查找所有父类型(基类和接口)
例如,如果我有
class A : B, C { }
class B : D { }
interface C : E { }
class D { }
interface E { }
我希望看到一个是 B C d和E和对象
请告诉我要做到这一点的最好方法是什么?有没有一种反射方法来做到这一点,或者我是否需要让自己做点什么。
====编辑====
所以像这样?
public static IEnumerable<Type> ParentTypes(this Type type)
{
foreach (Type i in type.GetInterfaces())
{
yield return i;
foreach (Type t in i.ParentTypes())
{
yield return t;
}
}
if (type.BaseType != null)
{
yield return type.BaseType;
foreach (Type b in type.BaseType.ParentTypes())
{
yield return b;
}
}
}
我有点儿希望我不必自己做,但哦。