Type.GetNestedTypesとは? わかりやすく解説

Type.GetNestedTypes メソッド ()

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

現在の型で直接入れ子になっているパブリック型だけが返されます。検索は再帰的ではありません。

型に対するリフレクション時に Get メソッドによって返される基本クラスのメンバを次の表に示します。

  1. 名前と署名による隠ぺいでは、カスタム修飾子、戻り値の型、パラメータの型、sentinel、およびアンマネージ呼び出し規約を含めて、署名のすべての部分が判断の対象となります。これはバイナリ比較です。

  2. リフレクションの場合、プロパティおよびイベントは名前と署名によって隠ぺいされています。基本クラスに get アクセサと set アクセサの両方を持つプロパティがあり、派生クラスには get アクセサしかない場合、派生クラスのプロパティにより基本クラスのプロパティが隠ぺいされ、基本クラスの set アクセサにはアクセスできません。

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

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

入れ子にされた型がジェネリック型である場合、このメソッドはそのジェネリック型定義を返します。包含するジェネリック型がクローズ構築型の場合も同様です。

メモメモ

現在の Type が C#、isual Basic、または C++ で定義されたジェネリック型を表す場合、入れ子にされた型はそれ自体のジェネリック パラメータが存在しなくてもすべてジェネリック型です。ただし、入れ子にされた型が動的アセンブリで定義された場合、または MSIL アセンブラ (Ilasm.exe) によってコンパイルされた場合は、ジェネリック型であるとは限りません。

入れ子になったジェネリック型の詳細、および入れ子になったジェネリック型をジェネリック型定義から構築する方法の詳細については、MakeGenericType のトピックを参照してください。

使用例使用例

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

Imports System
Imports System.Reflection

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

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

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 'Main
End Class 'MyMainClass
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);  
        }         
    }
}
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 );
   }
}
import System.*;
import System.Reflection.*;

public class MyClass
{
    public static class
 NestClass
    {
        public static int
 myPublicInt = 0;
    } //NestClass

    public static class
 NestStruct
    {
        public static int
 myPublicInt = 0;
    } //NestStruct
} //MyClass

public class MyMainClass
{
    public static void main(String[]
 args)
    {
        try {
            // Get the Type object corresponding to MyClass.
            Type myType = MyClass.class.ToType();
            // Get an array of nested type objects in MyClass. 
            Type nestType[] = myType.GetNestedTypes();
            Console.WriteLine("The number of nested types is {0}.",
                System.Convert.ToString(nestType.get_Length()));
            for (int iCtr = 0; iCtr < nestType.get_Length();
 iCtr++) {
                Type t = nestType[iCtr];
                Console.WriteLine("Nested type is {0}.", t.ToString());
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("Error" + e.get_Message());
        }
    } //main
} //MyMainClass
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Type.GetNestedTypes メソッド (BindingFlags)

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

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public MustOverride Function
 GetNestedTypes ( _
    bindingAttr As BindingFlags _
) As Type()
Dim instance As Type
Dim bindingAttr As BindingFlags
Dim returnValue As Type()

returnValue = instance.GetNestedTypes(bindingAttr)
public abstract Type[] GetNestedTypes (
    BindingFlags bindingAttr
)
public:
virtual array<Type^>^ GetNestedTypes (
    BindingFlags bindingAttr
) abstract
public abstract Type[] GetNestedTypes (
    BindingFlags bindingAttr
)
public abstract function GetNestedTypes (
    bindingAttr : BindingFlags
) : Type[]

パラメータ

bindingAttr

検索の実行方法を指定する 1 つ以上の BindingFlags から成るビット マスク。

または

null 参照 (Visual Basic では Nothing) を返す 0。

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

解説解説

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

次の BindingFlags フィルタ フラグは、入れ子にされた型で、検索対象に含める型を定義するために使用できます。

このメソッドは、現在の型の入れ子にされた型のみを返します。継承型の階層は検索しません。継承型に入れ子にされた型を見つけるには、継承階層をウォークする必要があります。

BindingFlags.Instance と BindingFlags.Static は無視されます。

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

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

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

入れ子にされた型がジェネリック型である場合、このメソッドはそのジェネリック型定義を返します。包含するジェネリック型がクローズ構築型の場合も同様です。

メモメモ

現在の Type が C#、isual Basic、または C++ で定義されたジェネリック型を表す場合、入れ子にされた型はそれ自体のジェネリック パラメータが存在しなくてもすべてジェネリック型です。ただし、入れ子にされた型が動的アセンブリで定義された場合、または MSIL アセンブラ (Ilasm.exe) によってコンパイルされた場合は、ジェネリック型であるとは限りません。

入れ子になったジェネリック型の詳細、および入れ子になったジェネリック型をジェネリック型定義から構築する方法の詳細については、MakeGenericType のトピックを参照してください。

使用例使用例

入れ子になったパブリック クラスとプロテクト クラスをそれぞれ 2 つずつ作成し、指定したバインディング制約に一致するクラスの情報を表示する例を次に示します。

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

' Create a class with name 'MyTypeClass' with three properties.
Public Class MyTypeClass

    Public Class Myclass1
    End Class 'Myclass1


    Public Class Myclass2
    End Class 'Myclass2


    Protected Class MyClass3
    End Class 'MyClass3

    Protected Class MyClass4
    End Class 'MyClass4
End Class 'MyTypeClass


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
 Or BindingFlags.Instance))
        Console.WriteLine("The number of public nested classes
 is {0}.", myTypeArray.Length.ToString())
        ' Display all the public nested classes.
        DisplayTypeInfo(myTypeArray)
        ' Get the nonpublic nested classes.
        Dim myTypeArray1 As Type() = myType.GetNestedTypes((BindingFlags.NonPublic
 Or BindingFlags.Instance))
        Console.WriteLine("The number of protected nested classes
 is {0}.", myTypeArray1.Length.ToString())
        ' Display  the information for all nested classes.
        DisplayTypeInfo(myTypeArray1)
    End Sub 'Main

    Public Shared Sub DisplayTypeInfo(ByVal
 myArrayType() As Type)
        ' Display the information for all nested classes.
        Dim i As Integer
        For i = 0 To myArrayType.Length - 1
            Dim myType As Type = CType(myArrayType(i),
 Type)
            Console.WriteLine("The name of the nested class is
 {0}.", myType.ToString())
        Next i
    End Sub 'DisplayTypeInfo
End Class 'TypeMain 
using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class with two nested public classes and two 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|BindingFlags.Instance);
        Console.WriteLine("The number of nested public classes
 is {0}.", myTypeArray.Length);
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        // 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.
        for(int i=0;i<myArrayType.Length;i++)
        {
            Type myType = (Type)myArrayType[i];
            Console.WriteLine("The name of the nested class
 is {0}.", myType.ToString());
        }
    }
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

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

private:
   ref class Myclass2{};

protected:
   ref class MyClass3{};

private:
   ref class MyClass4{};
};

void DisplayTypeInfo( array<Type^>^myArrayType )
{
   // Display the information for all the nested classes.
   for ( int i = 0; i < myArrayType->Length;
 i++ )
   {
      Type^ myType = myArrayType[ i ];
      Console::WriteLine( "The name of the nested class is
 {0}.", myType );

   }
}

int main()
{
   Type^ myType = MyTypeClass::typeid;
   
   // Get the public nested classes.
   array<Type^>^myTypeArray = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::Public
 | BindingFlags::Instance) );
   Console::WriteLine( "The number of nested public classes
 is {0}.", myTypeArray->Length );
   
   // Display all the public nested classes.
   DisplayTypeInfo( myTypeArray );
   
   // Get the nonpublic nested classes.
   array<Type^>^myTypeArray1 = myType->GetNestedTypes( static_cast<BindingFlags>(BindingFlags::NonPublic
 | BindingFlags::Instance) );
   Console::WriteLine( "The number of nested protected classes
 is {0}.", myTypeArray1->Length );
   
   // Display all the nonpublic nested classes.
   DisplayTypeInfo( myTypeArray1 );
}
import System.*;
import System.Reflection.*;
import System.Reflection.Emit.*;

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

public class TypeMain
{
    public static void main(String[]
 args)
    {
        Type myType = MyTypeClass.class.ToType();
        // Get the public nested classes.
        Type myTypeArray[] = myType.GetNestedTypes(BindingFlags.Public |
            BindingFlags.Instance);
        Console.WriteLine("The number of nested public classes
 is {0}.",
            (Int32)myTypeArray.get_Length());
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        // Get the nonpublic nested classes.
        Type myTypeArray1[] = myType.GetNestedTypes(BindingFlags.NonPublic |
            BindingFlags.Instance);
        Console.WriteLine("The number of nested protected
 classes is {0}.",
            (Int32)myTypeArray1.get_Length());
        // Display all the nonpublic nested classes.
        DisplayTypeInfo(myTypeArray1);
    } //main

    public static void DisplayTypeInfo(Type
 myArrayType[])
    {
        // Display the information for all the nested classes.
        for (int i = 0; i < myArrayType.get_Length();
 i++) {
            Type myType = (Type)myArrayType.get_Item(i);
            Console.WriteLine("The name of the nested class
 is {0}.",
                myType.ToString());
        }
    } //DisplayTypeInfo
} //TypeMain
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Type クラス
Type メンバ
System 名前空間
BindingFlags
DefaultBinder
GetNestedType

Type.GetNestedTypes メソッド


_Type.GetNestedTypes メソッド




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Type.GetNestedTypes」の関連用語

Type.GetNestedTypesのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Type.GetNestedTypesのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS