2012-01-15 53 views
9

我希望能夠找到特定類型的所有父類型(基類和接口)。查找所有父類型(基類和接口)

例如,如果我有

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; 
      } 
     } 
    } 

我有點兒希望我不必自己做,但哦。

回答

19

更通用的解決方案:

public static bool InheritsFrom(this Type type, Type baseType) 
{ 
    // null does not have base type 
    if (type == null) 
    { 
     return false; 
    } 

    // only interface can have null base type 
    if (baseType == null) 
    { 
     return type.IsInterface; 
    } 

    // check implemented interfaces 
    if (baseType.IsInterface) 
    { 
     return type.GetInterfaces().Contains(baseType); 
    } 

    // check all base types 
    var currentType = type; 
    while (currentType != null) 
    { 
     if (currentType.BaseType == baseType) 
     { 
      return true; 
     } 

     currentType = currentType.BaseType; 
    } 

    return false; 
} 

或者真正得到所有的父類:

public static IEnumerable<Type> GetParentTypes(this Type type) 
{ 
    // is there any base type? 
    if ((type == null) || (type.BaseType == null)) 
    { 
     yield break; 
    } 

    // return all implemented or inherited interfaces 
    foreach (var i in type.GetInterfaces()) 
    { 
     yield return i; 
    } 

    // return all inherited types 
    var currentBaseType = type.BaseType; 
    while (currentBaseType != null) 
    { 
     yield return currentBaseType; 
     currentBaseType= currentBaseType.BaseType; 
    } 
} 
6

要獲得按類型實現的接口,請使用Type.GetInterfaces。要查看它的類層次結構,您可以反覆使用Type.BaseType,直到您點擊null-參考(通常會在您點擊System.Object後發生,但不一定 - 例如,接口類型的基本類型將直接爲null)。

5

A C對於懶人#擴展方法:

/// <summary> 
/// Extension method to check the entire inheritance hierarchy of a 
/// type to see whether the given base type is inherited. 
/// </summary> 
/// <param name="t">The Type object this method was called on</param> 
/// <param name="baseType">The base type to look for in the 
/// inheritance hierarchy</param> 
/// <returns>True if baseType is found somewhere in the inheritance 
/// hierarchy, false if not</returns> 
public static bool InheritsFrom(this Type t, Type baseType) 
{ 
    Type cur = t.BaseType; 

    while (cur != null) 
    { 
     if (cur.Equals(baseType)) 
     { 
      return true; 
     } 

     cur = cur.BaseType; 
    } 

    return false; 
} 
1
public static bool IsSubclassOfTypeOrInterface(this Type type, Type ofTypeOrInterface) 
{ 
    if (type == null) 
    { 
     throw new ArgumentNullException("type"); 
    } 
    if (ofTypeOrInterface == null) 
    { 
     throw new ArgumentNullException("ofTypeOrInterface"); 
    } 

    return ofTypeOrInterface.IsInterface 
       ? type.GetInterfaces().Contains(ofTypeOrInterface) 
       : type.IsSubclassOf(ofTypeOrInterface); 
}