Type.GetNestedTypes メソッド

定義

現在の Type 内で入れ子になっている型を取得します。

オーバーロード

GetNestedTypes()

現在の Type 内で入れ子になっているすべてのパブリック型を返します。

GetNestedTypes(BindingFlags)

派生クラス内でオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type 内で入れ子になっている型を検索します。

GetNestedTypes()

現在の Type 内で入れ子になっているすべてのパブリック型を返します。

public:
 cli::array <Type ^> ^ GetNestedTypes();
public:
 virtual cli::array <Type ^> ^ GetNestedTypes();
public Type[] GetNestedTypes ();
member this.GetNestedTypes : unit -> Type[]
abstract member GetNestedTypes : unit -> Type[]
override this.GetNestedTypes : unit -> Type[]
Public Function GetNestedTypes () As Type()

戻り値

Type[]

現在の Type 内で入れ子になっているパブリック型を表す Type オブジェクトの配列 (検索は非再帰的)。または、現在の Type で入れ子になっているパブリック型がない場合は、Type 型の空の配列。

実装

次の例では、 でMyClass入れ子になったクラスと をstruct定義し、 の型を使用して入れ子になった型のMyClassオブジェクトを取得します。

using namespace System;
using namespace System::Reflection;
public ref class MyClass
{
public:
   ref class NestClass
   {
      public:
         static int myPublicInt = 0;
   };

   ref struct NestStruct
   {
      public:
         static int myPublicInt = 0;
   };
};

int main()
{
   try
   {
      // Get the Type object corresponding to MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get an array of nested type objects in MyClass.
      array<Type^>^nestType = myType->GetNestedTypes();
      Console::WriteLine( "The number of nested types is {0}.", nestType->Length );
      System::Collections::IEnumerator^ myEnum = nestType->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Type^ t = safe_cast<Type^>(myEnum->Current);
         Console::WriteLine( "Nested type is {0}.", t );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Error {0}", e->Message );
   }
}
using System;
using System.Reflection;
public class MyClass
{
    public class NestClass
    {
        public static int myPublicInt=0;
    }
    public struct NestStruct
    {
        public static int myPublicInt=0;
    }
}

public class MyMainClass
{
    public static void Main()
    {
        try
        {
            // Get the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);
            // Get an array of nested type objects in MyClass.
            Type[] nestType=myType.GetNestedTypes();
            Console.WriteLine("The number of nested types is {0}.", nestType.Length);
            foreach(Type t in nestType)
                Console.WriteLine("Nested type is {0}.", t.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Error"+e.Message);
        }
    }
}
Imports System.Reflection

Public Class MyClass1
    Public Class NestClass
        Public Shared myPublicInt As Integer = 0
    End Class

    Public Structure NestStruct
        Public myPublicInt As Integer
    End Structure 'NestStruct
End Class

Public Class MyMainClass
    Public Shared Sub Main()
        Try
            ' Get the Type object corresponding to MyClass.
            Dim myType As Type = GetType(MyClass1)
            ' Get an array of nested type objects in MyClass.                 
            Dim nestType As Type() = myType.GetNestedTypes()
            Console.WriteLine("The number of nested types is {0}.", nestType.Length)
            Dim t As Type
            For Each t In nestType
                Console.WriteLine("Nested type is {0}.", t.ToString())
            Next t
        Catch e As Exception
            Console.WriteLine("Error", e.Message.ToString())
        End Try
    End Sub
End Class

注釈

.NET 6 以前のバージョンでは、 GetNestedTypes メソッドは、アルファベット順や宣言順序などの特定の順序で型を返しません。 コードは、その順序が異なるため、型が返される順序に依存してはなりません。 ただし、.NET 7 以降では、アセンブリ内のメタデータの順序に基づいて順序が決定論的になります。

現在の型ですぐに入れ子になったパブリック型のみが返されます。検索は再帰的ではありません。

次の表は、型に反映するときに メソッドによって Get 返される基底クラスのメンバーを示しています。

メンバーの型 静的 非静的
コンストラクター いいえ いいえ
フィールド いいえ はい。 フィールドは常に名前と署名で非表示になります。
Event 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドの継承と同じであるという点です。 リフレクションでは、プロパティは名前と署名による非表示として扱います。 以下の注 2 を参照してください。
メソッド いいえ はい。 メソッド (仮想と非仮想の両方) は、名前で非表示にすることも、名前と署名で非表示にすることもできます。
入れ子になった型 いいえ いいえ
プロパティ 適用なし 一般的な型システムルールは、継承が プロパティを実装するメソッドの継承と同じであるという点です。 リフレクションでは、プロパティは名前と署名による非表示として扱います。 以下の注 2 を参照してください。
  1. 名前と署名による非表示は、カスタム修飾子、戻り値の型、パラメーター型、sentinel、アンマネージド呼び出し規則など、署名のすべての部分を考慮します。 これはバイナリ比較です。

  2. リフレクションの場合、プロパティとイベントは名前と署名によって非表示になります。 基底クラスに get アクセサーと set アクセサーの両方を持つプロパティがあるが、派生クラスに get アクセサーしかない場合、派生クラス プロパティは基底クラス プロパティを非表示にし、基底クラスのセッターにアクセスすることはできません。

  3. カスタム属性は、共通型システムの一部ではありません。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約の入れ子になった型を検索します。

入れ子になった型がジェネリックの場合、このメソッドはジェネリック型定義を返します。 これは、外側のジェネリック型が閉じた構築型である場合でも当てはまります。

Note

現在 Type の が C#、Visual Basic、または C++ で定義されているジェネリック型を表す場合、入れ子になった型は、独自のジェネリック パラメーターがない場合でも、すべてジェネリックになります。 これは、動的アセンブリで定義された入れ子になった型や 、Ilasm.exe (IL アセンブラー) を使用してコンパイルされた入れ子になった型に当てはまるとは限りません。

入れ子になったジェネリック型と、そのジェネリック型定義から入れ子になったジェネリック型を構築する方法については、「」を参照してください MakeGenericType

こちらもご覧ください

適用対象

GetNestedTypes(BindingFlags)

派生クラス内でオーバーライドされた場合、指定したバインディング制約を使用して、現在の Type 内で入れ子になっている型を検索します。

public:
 abstract cli::array <Type ^> ^ GetNestedTypes(System::Reflection::BindingFlags bindingAttr);
public abstract Type[] GetNestedTypes (System.Reflection.BindingFlags bindingAttr);
abstract member GetNestedTypes : System.Reflection.BindingFlags -> Type[]
Public MustOverride Function GetNestedTypes (bindingAttr As BindingFlags) As Type()

パラメーター

bindingAttr
BindingFlags

検索を実施する方法を指定する列挙値のビットごとの組み合わせ。

または

null を返す場合は Default

戻り値

Type[]

指定したバインディング制約と一致する現在の Type で入れ子にされたすべての型を表す Type オブジェクトの配列 (検索は非再帰的)。または、バインディング制約と一致する入れ子にされた型が見つからない場合は、Type 型の空の配列。

実装

次の例では、2 つの入れ子になったパブリック クラスと 2 つの入れ子になった保護されたクラスを作成し、指定されたバインド制約に一致するクラスの情報を表示します。

using namespace System;
using namespace System::Reflection;

// Create a class with two nested public classes and two nested protected classes.
public ref class MyTypeClass
{
   public:
      ref class Myclass1{};
   
   public:
      ref class Myclass2{};
   
   protected:
      ref class MyClass3{};
   
   protected:
      ref class MyClass4{};
};

void DisplayTypeInfo(array<Type^>^ myArrayType)
{
   // Display the information for all the nested classes.
   for each (Type^ t in myArrayType)
      Console::WriteLine( "The name of the nested class is {0}.", t->FullName);
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public nested classes.
   array<Type^>^myTypeArray = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::Public));
   Console::WriteLine( "The number of nested public classes is {0}.", myTypeArray->Length );
  
   // Display all the public nested classes.
   DisplayTypeInfo( myTypeArray );
   Console::WriteLine();
   
   // Get the nonpublic nested classes.
   array<Type^>^myTypeArray1 = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::NonPublic));
   Console::WriteLine( "The number of nested protected classes is {0}.", myTypeArray1->Length );
   
   // Display all the nonpublic nested classes.
   DisplayTypeInfo( myTypeArray1 );
}
// The example displays the following output:
//       The number of public nested classes is 2.
//       The name of the nested class is MyTypeClass+Myclass1.
//       The name of the nested class is MyTypeClass+Myclass2.
//       
//       The number of protected nested classes is 2.
//       The name of the nested class is MyTypeClass+MyClass3.
//       The name of the nested class is MyTypeClass+MyClass4.
using System;
using System.Reflection;

// Create a class with 2 nested public and 2 nested protected classes.
public class MyTypeClass
{
    public class Myclass1
    {
    }

    public class Myclass2
    {
    }

    protected class MyClass3
    {
    }

    protected class MyClass4
    {
    }
}

public class TypeMain
{
    public static void Main()
    {
        Type myType = (typeof(MyTypeClass));
        // Get the public nested classes.
        Type[] myTypeArray = myType.GetNestedTypes(BindingFlags.Public);
        Console.WriteLine("The number of nested public classes is {0}.", myTypeArray.Length);
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        Console.WriteLine();

        // Get the nonpublic nested classes.
        Type[] myTypeArray1 = myType.GetNestedTypes(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of nested protected classes is {0}.", myTypeArray1.Length);
        // Display all the nonpublic nested classes.
        DisplayTypeInfo(myTypeArray1);		
    }

    public static void DisplayTypeInfo(Type[] myArrayType)
    {
        // Display the information for all the nested classes.
        foreach (var t in myArrayType)
            Console.WriteLine("The name of the nested class is {0}.", t.FullName);
    }
}
// The example displays the following output:
//       The number of public nested classes is 2.
//       The name of the nested class is MyTypeClass+Myclass1.
//       The name of the nested class is MyTypeClass+Myclass2.
//
//       The number of protected nested classes is 2.
//       The name of the nested class is MyTypeClass+MyClass3.
//       The name of the nested class is MyTypeClass+MyClass4.
Imports System.Reflection

' Create a class with three properties.
Public Class MyTypeClass
    Public Class Myclass1
    End Class 

    Public Class Myclass2
    End Class 

    Protected Class MyClass3
    End Class 

    Protected Class MyClass4
    End Class 
End Class 

Public Class TypeMain
    Public Shared Sub Main()
        Dim myType As Type = GetType(MyTypeClass)

        ' Get the public nested classes.
        Dim myTypeArray As Type() = myType.GetNestedTypes((BindingFlags.Public))
        Console.WriteLine("The number of public nested classes is {0}.", myTypeArray.Length.ToString())
        ' Display all the public nested classes.
        DisplayTypeInfo(myTypeArray)
        Console.WriteLine()
        
        ' Get the nonpublic nested classes.
        Dim myTypeArray1 As Type() = myType.GetNestedTypes((BindingFlags.NonPublic))
        Console.WriteLine("The number of protected nested classes is {0}.", myTypeArray1.Length.ToString())
        ' Display  the information for all nested classes.
        DisplayTypeInfo(myTypeArray1)
    End Sub 

    Public Shared Sub DisplayTypeInfo(ByVal myArrayType() As Type)
        ' Display the information for all nested classes.
        For Each t In myArrayType
            Console.WriteLine("The name of the nested class is {0}.", t.FullName)
        Next 
    End Sub 
End Class  
' The example displays the following output:
'       The number of public nested classes is 2.
'       The name of the nested class is MyTypeClass+Myclass1.
'       The name of the nested class is MyTypeClass+Myclass2.
'       
'       The number of protected nested classes is 2.
'       The name of the nested class is MyTypeClass+MyClass3.
'       The name of the nested class is MyTypeClass+MyClass4.

注釈

入れ子になった型の検索は再帰的ではありません。

.NET 6 以前のバージョンでは、 GetNestedTypes メソッドは、アルファベット順や宣言順序などの特定の順序で型を返しません。 コードは、その順序が異なるため、型が返される順序に依存してはなりません。 ただし、.NET 7 以降では、アセンブリ内のメタデータの順序に基づいて順序が決定論的になります。

BindingFlags のフィルター フラグを使用して、検索に含める入れ子になった型を定義できます。

  • 戻り値を取得するには、 または BindingFlags.NonPublic のいずれかをBindingFlags.Public指定する必要があります。

  • パブリックの入れ子になった型を検索に含めるには、 を指定 BindingFlags.Public します。

  • を指定 BindingFlags.NonPublic して、パブリックでない入れ子になった型 (つまり、プライベート、内部、および保護された入れ子になった型) を検索に含めます。

このメソッドは、現在の型の入れ子になった型のみを返します。 現在の型の基底クラスは検索されません。 基底クラスで入れ子になっている型を見つけるには、各レベルで を呼び出して継承階層を歩く GetNestedTypes 必要があります。

BindingFlags.InstanceBindingFlags.Static は無視されます。

フラグのみを指定するか、 BindingFlags.Public フラグのみを指定してこのメソッドを BindingFlags.NonPublic 呼び出すと、指定された入れ子になった型が返され、他のフラグは必要ありません。

詳細については、「System.Reflection.BindingFlags」を参照してください。

現在 Type の がジェネリック型またはジェネリック メソッドの定義で型パラメーターを表している場合、このメソッドはクラス制約の入れ子になった型を検索します。

入れ子になった型がジェネリックの場合、このメソッドはジェネリック型定義を返します。 これは、外側のジェネリック型が閉じた構築型である場合でも当てはまります。

Note

現在 Type の が C#、Visual Basic、または C++ で定義されているジェネリック型を表す場合、入れ子になった型は、独自のジェネリック パラメーターがない場合でも、すべてジェネリックになります。 これは、動的アセンブリで定義された入れ子になった型や 、Ilasm.exe (IL アセンブラー) を使用してコンパイルされた入れ子になった型に当てはまるとは限りません。

入れ子になったジェネリック型と、そのジェネリック型定義から入れ子になったジェネリック型を構築する方法については、「」を参照してください MakeGenericType

こちらもご覧ください

適用対象