我希望能夠找到特定類型的所有父類型(基類和接口)。查找所有父類型(基類和接口)
例如,如果我有
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;
}
}
}
我有點兒希望我不必自己做,但哦。