Type.GetNestedTypes Yöntem

Tanım

Geçerli Typeiçinde iç içe türleri alır.

Aşırı Yüklemeler

GetNestedTypes()

Geçerli Typeiçinde iç içe yerleştirilmiş genel türleri döndürür.

GetNestedTypes(BindingFlags)

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak geçerli Typeiçinde iç içe geçmiş türleri arar.

GetNestedTypes()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Geçerli Typeiçinde iç içe yerleştirilmiş genel türleri döndürür.

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

Döndürülenler

Type[]

Geçerli Type içinde iç içe yerleştirilmiş ortak türleri temsil eden bir nesne dizisi Type (arama özyinelemeli değildir) veya geçerli Typeiçinde genel türler iç içe yerleştirilmemişse boş bir tür Type dizisi.

Uygulamalar

Örnekler

Aşağıdaki örnek, içinde MyClassve iç içe bir sınıfını struct tanımlar ve ardından türünü kullanarak iç içe geçmiş türlerin MyClassnesnelerini alır.

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

Açıklamalar

.NET 6 ve önceki sürümlerde yöntemi, GetNestedTypes alfabetik veya bildirim sırası gibi belirli bir sırada türleri döndürmez. Kodunuz türlerin döndürülme sırasına bağlı olmamalıdır, çünkü bu sıra değişir. Bununla birlikte, .NET 7'den başlayarak sıralama, derlemedeki meta veri sıralamasını temel alarak belirleyicidir.

Yalnızca geçerli türe hemen iç içe yerleştirilmiş genel türler döndürülür; arama özyinelemeli değil.

Aşağıdaki tabloda, bir türe yansıtılırken yöntemler tarafından Get döndürülen temel sınıfın üyeleri gösterilmektedir.

Üye Türü Statik Statik Olmayan
Oluşturucu Hayır Hayır
Alan Hayır Evet. Bir alan her zaman "ada ve imzaya göre gizle" özelliğindedir.
Olay Uygulanamaz Ortak tür sistemi kuralı, devralma işleminin özelliği uygulayan yöntemlerinkiyle aynı olmasıdır. Yansıma, özellikleri "ada ve imzaya göre gizle" şeklinde kullanır. Aşağıdaki 2. nota bakın.
Yöntem Hayır Evet. Bir yöntem (sanal ve sanal olmayan) "ada göre gizle" veya "ada ve imzaya göre gizle" özelliğinde olabilir.
İç İçe Tür Hayır Hayır
Özellik Uygulanamaz Ortak tür sistemi kuralı, devralma işleminin özelliği uygulayan yöntemlerinkiyle aynı olmasıdır. Yansıma, özellikleri "ada ve imzaya göre gizle" şeklinde kullanır. Aşağıdaki 2. nota bakın.
  1. "Ada ve imzaya göre gizle" özelliği, imzanın tüm parçalarını (özel değiştiriciler, dönüş türleri, parametre türleri, başlangıç/bitiş simgeleri ve yönetilmeyen çağrı kuralları dahil) dikkate alır. Bu ikili bir karşılaştırmadır.

  2. Yansıma için, özellikler ve olaylar "ada ve imzaya göre gizle" özelliğindedir. Bu temel sınıfta hem alma hem de ayarlama erişimcisine sahip bir özelliğiniz varsa, ancak türetilen sınıfın yalnızca bir alma erişimcisi varsa, türetilen sınıf özelliği temel sınıf özelliğini gizler ve siz de temel sınıfta ayarlayıcıya erişemezsiniz.

  3. Özel öznitelikler ortak tür sisteminin parçası değildir.

Geçerli Type , genel bir türün veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının iç içe geçmiş türlerini arar.

İç içe geçmiş bir tür genel ise, bu yöntem genel tür tanımını döndürür. Kapsayan genel tür kapalı bir oluşturulmuş tür olsa bile bu durum geçerlidir.

Not

Geçerli Type , C#, Visual Basic veya C++ ile tanımlanan genel bir türü temsil ediyorsa, kendi genel parametreleri olmasa bile iç içe türlerin tümü geneldir. Bu durum dinamik derlemelerde tanımlanan veya Ilasm.exe (IL Assembler) ile derlenen iç içe türler için geçerli olmayabilir.

İç içe genel türler ve kendi genel tür tanımlarından iç içe genel türler oluşturma hakkında bilgi için bkz MakeGenericType. .

Ayrıca bkz.

Şunlara uygulanır

GetNestedTypes(BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak geçerli Typeiçinde iç içe geçmiş türleri arar.

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

Parametreler

bindingAttr
BindingFlags

Aramanın nasıl yürütüleceğini belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürecek null.

Döndürülenler

Type[]

Belirtilen bağlama kısıtlamalarıyla eşleşen geçerli Type iç içe geçmiş tüm türleri temsil eden bir nesne dizisi Type (arama özyinelemeli değildir) veya bağlama kısıtlamalarıyla eşleşen iç içe geçmiş türler bulunamazsa türünde boş bir diziType.

Uygulamalar

Örnekler

Aşağıdaki örnek, iki iç içe genel sınıf ve iki iç içe korumalı sınıf oluşturur ve belirtilen bağlama kısıtlamalarıyla eşleşen sınıfların bilgilerini görüntüler.

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.

Açıklamalar

İç içe türler için arama özyinelemeli değildir.

.NET 6 ve önceki sürümlerde yöntemi, GetNestedTypes alfabetik veya bildirim sırası gibi belirli bir sırada türleri döndürmez. Kodunuz türlerin döndürülme sırasına bağlı olmamalıdır, çünkü bu sıra değişir. Bununla birlikte, .NET 7'den başlayarak sıralama, derlemedeki meta veri sıralamasını temel alarak belirleyicidir.

Aşağıdaki BindingFlags filtre bayrakları, aramaya hangi iç içe türlerin eklendiğini tanımlamak için kullanılabilir:

Bu yöntem yalnızca geçerli türün iç içe türlerini döndürür. Geçerli türün temel sınıflarını aramaz. Temel sınıflarda iç içe yerleştirilmiş türleri bulmak için, her düzeyde çağırarak GetNestedTypes devralma hiyerarşisinde yürümeniz gerekir.

BindingFlags.Instance ve BindingFlags.Static yoksayılır.

Bu yöntemi yalnızca BindingFlags.Public bayrakla veya yalnızca BindingFlags.NonPublic bayrakla çağırmak, belirtilen iç içe türleri döndürür ve başka bayrak gerektirmez.

Daha fazla bilgi edinmek için bkz. System.Reflection.BindingFlags.

Geçerli Type , genel bir türün veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının iç içe geçmiş türlerini arar.

İç içe geçmiş bir tür genel ise, bu yöntem genel tür tanımını döndürür. Kapsayan genel tür kapalı bir oluşturulmuş tür olsa bile bu durum geçerlidir.

Not

Geçerli Type , C#, Visual Basic veya C++ ile tanımlanan genel bir türü temsil ediyorsa, kendi genel parametreleri olmasa bile iç içe türlerin tümü geneldir. Bu durum dinamik derlemelerde tanımlanan veya Ilasm.exe (IL Assembler) ile derlenen iç içe türler için geçerli olmayabilir.

İç içe genel türler ve kendi genel tür tanımlarından iç içe genel türler oluşturma hakkında bilgi için bkz MakeGenericType. .

Ayrıca bkz.

Şunlara uygulanır