以下の内容はhttps://baba-s.hatenablog.com/entry/2022/09/27/090740より取得しました。


【C#】継承しているすべての基底クラスと実装しているすべてのインターフェイスの Type を返す拡張メソッド

概要

/// <summary>
/// 継承しているすべての基底クラスと実装しているすべてのインターフェイスの Type を返します
/// </summary>
public static IEnumerable<Type> GetParentTypes( this Type self )
{
    if ( self == null ) yield break;

    foreach ( var x in self.GetInterfaces() )
    {
        yield return x;
    }

    var currentBaseType = self.BaseType;

    while ( currentBaseType != null )
    {
        yield return currentBaseType;
        currentBaseType = currentBaseType.BaseType;
    }
}

使用例

foreach ( var parentType in typeof( List<int> ).GetParentTypes() )
{
    Debug.Log( parentType );
}

参考サイト様




以上の内容はhttps://baba-s.hatenablog.com/entry/2022/09/27/090740より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14