Type.GetNestedTypes Metode

Definisi

Mendapatkan jenis yang disarangkan dalam saat ini Type.

Overload

GetNestedTypes()

Mengembalikan jenis publik yang ditumpuk di saat ini Type.

GetNestedTypes(BindingFlags)

Saat ditimpa di kelas turunan, cari jenis yang ditumpuk di saat ini Type, menggunakan batasan pengikatan yang ditentukan.

GetNestedTypes()

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Mengembalikan jenis publik yang ditumpuk di saat ini 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()

Mengembalikan

Type[]

Array Type objek yang mewakili jenis publik yang ditumpuk di saat ini Type (pencarian tidak rekursif), atau array jenis Type kosong jika tidak ada jenis publik yang ditumpuk di saat ini Type.

Penerapan

Contoh

Contoh berikut mendefinisikan kelas berlapis dan struct di MyClass, lalu mendapatkan objek dari jenis berlapis menggunakan jenis 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

Keterangan

Dalam .NET 6 dan versi yang lebih lama, GetNestedTypes metode tidak mengembalikan jenis dalam urutan tertentu, seperti urutan alfabet atau deklarasi. Kode Anda tidak boleh bergantung pada urutan di mana jenis dikembalikan, karena urutan tersebut bervariasi. Namun, dimulai dengan .NET 7, pemesanan bersifat deterministik berdasarkan urutan metadata dalam perakitan.

Hanya jenis publik yang segera disarangkan dalam jenis saat ini yang dikembalikan; pencarian tidak rekursif.

Tabel berikut menunjukkan anggota kelas dasar apa yang dikembalikan oleh Get metode saat mencerminkan jenis.

Jenis anggota Statis Non-Statis
Konstruktor Tidak Tidak
Bidang Tidak Ya. Bidang selalu disembunyikan menurut nama dan tanda tangan.
Kejadian Tidak berlaku Aturan sistem jenis umum adalah bahwa pewarisan sama dengan metode yang mengimplementasikan properti . Pantulan memperlakukan properti sebagai hide-by-name-and-signature. Lihat catatan 2 di bawah ini.
Metode Tidak Ya. Metode (baik virtual maupun non-virtual) dapat berupa hide-by-name atau hide-by-name-and-signature.
Tipe Berlapis Tidak Tidak
Properti Tidak berlaku Aturan sistem jenis umum adalah bahwa pewarisan sama dengan metode yang mengimplementasikan properti . Pantulan memperlakukan properti sebagai hide-by-name-and-signature. Lihat catatan 2 di bawah ini.
  1. Hide-by-name-and-signature mempertimbangkan semua bagian tanda tangan, termasuk pengubah kustom, jenis pengembalian, jenis parameter, sentinel, dan konvensi panggilan yang tidak dikelola. Ini adalah perbandingan biner.

  2. Untuk refleksi, properti dan peristiwa adalah hide-by-name-and-signature. Jika Anda memiliki properti dengan aksesor get dan set di kelas dasar, tetapi kelas turunan hanya memiliki aksesor get, properti kelas turunan menyembunyikan properti kelas dasar, dan Anda tidak akan dapat mengakses setter pada kelas dasar.

  3. Atribut kustom bukan bagian dari sistem jenis umum.

Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini mencari jenis berlapis dari batasan kelas.

Jika jenis berlapis umum, metode ini mengembalikan definisi jenis generiknya. Ini berlaku bahkan jika jenis generik yang disertakan adalah jenis konstruksi tertutup.

Catatan

Jika saat ini Type mewakili jenis generik yang ditentukan dalam C#, Visual Basic, atau C++, jenis berlapisnya semuanya generik meskipun tidak memiliki parameter generik sendiri. Ini belum tentu berlaku untuk jenis berlapis yang didefinisikan dalam rakitan dinamis atau dikompilasi dengan Ilasm.exe (Il Assembler).

Untuk informasi tentang jenis generik berlapis, dan tentang membangun jenis generik berlapis dari definisi jenis generiknya, lihat MakeGenericType.

Lihat juga

Berlaku untuk

GetNestedTypes(BindingFlags)

Sumber:
Type.cs
Sumber:
Type.cs
Sumber:
Type.cs

Saat ditimpa di kelas turunan, cari jenis yang ditumpuk di saat ini Type, menggunakan batasan pengikatan yang ditentukan.

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

Parameter

bindingAttr
BindingFlags

Kombinasi bitwise dari nilai enumerasi yang menentukan bagaimana pencarian dilakukan.

-atau-

Default untuk mengembalikan null.

Mengembalikan

Type[]

Array Type objek yang mewakili semua jenis yang ditumpuk dalam saat ini Type yang cocok dengan batasan pengikatan yang ditentukan (pencarian tidak rekursif), atau array tipe Typekosong , jika tidak ada jenis berlapis yang ditemukan yang cocok dengan batasan pengikatan.

Penerapan

Contoh

Contoh berikut membuat dua kelas publik berlapis dan dua kelas yang dilindungi berlapis, dan menampilkan informasi untuk kelas yang cocok dengan batasan pengikatan yang ditentukan.

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.

Keterangan

Pencarian untuk jenis berlapis tidak rekursif.

Dalam .NET 6 dan versi yang lebih lama, GetNestedTypes metode tidak mengembalikan jenis dalam urutan tertentu, seperti urutan alfabet atau deklarasi. Kode Anda tidak boleh bergantung pada urutan di mana jenis dikembalikan, karena urutan tersebut bervariasi. Namun, dimulai dengan .NET 7, pemesanan bersifat deterministik berdasarkan urutan metadata dalam perakitan.

Bendera filter berikut BindingFlags dapat digunakan untuk menentukan jenis berlapis mana yang akan disertakan dalam pencarian:

Metode ini hanya mengembalikan jenis berlapis dari jenis saat ini. Ini tidak mencari kelas dasar dari jenis saat ini. Untuk menemukan jenis yang ditumpuk di kelas dasar, Anda harus menjalankan hierarki pewarisan, memanggil GetNestedTypes di setiap tingkat.

BindingFlags.Instance dan BindingFlags.Static diabaikan.

Memanggil metode ini hanya BindingFlags.Public dengan bendera atau hanya BindingFlags.NonPublic bendera yang akan mengembalikan jenis berlapis yang ditentukan dan tidak memerlukan bendera lain.

Lihat System.Reflection.BindingFlags untuk informasi lebih lanjut.

Jika saat ini Type mewakili parameter jenis dalam definisi jenis generik atau metode generik, metode ini mencari jenis berlapis dari batasan kelas.

Jika jenis berlapis umum, metode ini mengembalikan definisi jenis generiknya. Ini berlaku bahkan jika jenis generik yang disertakan adalah jenis konstruksi tertutup.

Catatan

Jika saat ini Type mewakili jenis generik yang ditentukan dalam C#, Visual Basic, atau C++, jenis berlapisnya semuanya generik meskipun tidak memiliki parameter generik sendiri. Ini belum tentu berlaku untuk jenis berlapis yang didefinisikan dalam rakitan dinamis atau dikompilasi dengan Ilasm.exe (Il Assembler).

Untuk informasi tentang jenis generik berlapis, dan tentang membangun jenis generik berlapis dari definisi jenis generiknya, lihat MakeGenericType.

Lihat juga

Berlaku untuk