Share via


Type.GetNestedTypes メソッド

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

オーバーロードの一覧

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

[Visual Basic] Overloads Public Function GetNestedTypes() As Type()

[C#] public Type[] GetNestedTypes();

[C++] public: Type* GetNestedTypes() [];

[JScript] public function GetNestedTypes() : Type[];

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public MustOverride Function GetNestedTypes(BindingFlags) As Type()

[C#] public abstract Type[] GetNestedTypes(BindingFlags);

[C++] public: virtual Type* GetNestedTypes(BindingFlags) [] = 0;

[JScript] public abstract function GetNestedTypes(BindingFlags) : Type[];

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、GetNestedTypes のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 

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 

[C#] 

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

[C++] 

#using <mscorlib.dll>

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 __gc class MyTypeClass {
public:
   __gc class Myclass1 {
   };
   __gc class Myclass2 {
   };
protected:
   __gc class MyClass3 {
   };
   __gc class MyClass4 {
   };
};

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

int main() {
   Type* myType =(__typeof(MyTypeClass));
   // Get the public nested classes.
   Type*  myTypeArray[] = myType->GetNestedTypes(
      static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Instance));
   Console::WriteLine(S"The number of nested public classes is {0}.",
      __box( myTypeArray->Length));
   // Display all the public nested classes.
   DisplayTypeInfo(myTypeArray);
   // Get the nonpublic nested classes.
   Type*  myTypeArray1[] = myType->GetNestedTypes(
      static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
   Console::WriteLine(S"The number of nested protected classes is {0}.",
      __box(  myTypeArray1->Length));
   // Display all the nonpublic nested classes.
   DisplayTypeInfo(myTypeArray1);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Type クラス | Type メンバ | System 名前空間