2012-01-15 10 views
9

特定のタイプのすべての親タイプ(基本クラスとインターフェース)を検索できます。すべての親タイプを見つける(基本クラスとインターフェースの両方)

EG私は BのCのDであることを確認したいと最良の方法はこれを行うにいただきましたEとは

オブジェクト

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

を持っている場合は?これを行うためのリフレクション方法はありますか、私は何かを作る必要があります。

==== EDIT ====

だからこのような何か?

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を使用します。そのクラス階層を表示するには、null参照(通常はSystem.Objectをヒットした後に発生しますが、必ずしもそうではありません。たとえば、インターフェイスタイプの基本タイプが直接null)になるまで、Type.BaseTypeを繰り返し使用できます。

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); 
} 
関連する問題