2012-01-15 35 views
9

Tôi muốn có thể tìm thấy tất cả các loại cha mẹ (lớp cơ sở và giao diện) cho một loại cụ thể.Tìm tất cả các loại cha (cả hai lớp cơ sở và giao diện)

EG nếu tôi có

class A : B, C { } 
class B : D { } 
interface C : E { } 
class D { } 
interface E { } 

tôi muốn thấy rằng A B C D và E và Object

Whats cách tốt nhất để làm điều này? là có một phương pháp phản ánh để làm điều này hay tôi cần phải làm cho bản thân mình một cái gì đó.

==== EDIT ====

Vì vậy, một cái gì đó như thế này?

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

Tôi đã hy vọng tôi không phải tự làm điều đó mà là tốt.

Trả lời

19

Nhiều giải pháp chung:

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

Hoặc để thực sự có được tất cả các loại phụ huynh:

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

Để nhận giao diện được triển khai theo loại, hãy sử dụng Type.GetInterfaces. Để xem phân cấp lớp, bạn có thể sử dụng Type.BaseType lặp lại cho đến khi bạn nhấn null -reference (thường điều này sẽ xảy ra sau khi bạn nhấn System.Object, nhưng không nhất thiết - ví dụ: loại cơ sở loại giao diện sẽ trực tiếp là null).

5

Một C# phương pháp khuyến nông cho người lười biếng:

/// <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); 
} 
Các vấn đề liên quan