2012-01-15 26 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); 
}