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

Type.GetNestedTypes メソッド ()

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

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

解説解説

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

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

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

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

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

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

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

メモメモ

現在の TypeC#、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.InstanceBindingFlags.Static無視されます。

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

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

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

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

メモメモ

現在の TypeC#、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 メソッド




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Type.GetNestedTypes」の関連用語

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

   

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



Type.GetNestedTypesのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS