Type.GetNestedTypes 方法

定義

讓其在目前的 Type 內變成巢狀之類型。

多載

GetNestedTypes()

傳回在目前 Type 內形成巢狀的公用類型。

GetNestedTypes(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋在目前 Type 內形成巢狀的類型。

GetNestedTypes()

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

傳回在目前 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()

傳回

Type[]

Type 物件的陣列,代表目前 Type 中的巢狀公用類型 (搜尋不是遞迴的);如果目前 Type 中沒有巢狀公用類型,則為 Type 類型的空陣列。

實作

範例

下列範例會在 中定義巢狀類別和 struct 中的 MyClass ,然後使用 的類型取得巢狀型 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

備註

在 .NET 6 和舊版中 GetNestedTypes ,方法不會以特定順序傳回類型,例如字母順序或宣告順序。 您的程式碼不得取決於傳回類型的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

只會傳回目前型別中立即巢狀的公用類型;搜尋不是遞迴的。

下表顯示反映類型時,方法會傳 Get 回基類的成員。

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱與簽章隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
方法 可以。 (虛擬和非虛擬) 的方法可以是依名稱隱藏或依名稱隱藏和簽章。
巢狀類型
屬性 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附注 2。
  1. 隱藏名稱與簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回型別、參數類型、sentinels 和 Unmanaged 呼叫慣例。 這是二進位比較。

  2. 對於反映,屬性和事件會依名稱與簽章隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。

  3. 自訂屬性不是一般型別系統的一部分。

如果目前 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的巢狀類型。

如果巢狀類型為泛型,這個方法會傳回其泛型型別定義。 即使封入泛型型別是封閉式建構型別,也是如此。

注意

如果目前 Type 代表 C#、Visual Basic 或 C++ 中定義的泛型型別,即使它們本身沒有泛型參數,其巢狀類型也是全部泛型。 這不一定是動態元件中定義的巢狀類型,也不一定是使用 Ilasm.exe (IL 組合器編譯)

如需巢狀泛型型別的相關資訊,以及從其泛型型別定義建構巢狀泛型型別的詳細資訊,請參閱 MakeGenericType

另請參閱

適用於

GetNestedTypes(BindingFlags)

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

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋在目前 Type 內形成巢狀的類型。

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

參數

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

傳回

Type[]

Type 物件的陣列,代表目前 Type 中符合指定繫結條件約束的所有巢狀類型 (搜尋不是遞迴的);如果找不到符合繫結條件約束的巢狀類型,則為 Type 類型的空陣列。

實作

範例

下列範例會建立兩個巢狀公用類別和兩個巢狀受保護類別,並顯示符合指定系結條件約束之類別的資訊。

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.

備註

巢狀類型的搜尋不是遞迴的。

在 .NET 6 和舊版中 GetNestedTypes ,方法不會以特定順序傳回類型,例如字母順序或宣告順序。 您的程式碼不得取決於傳回類型的順序,因為該順序會有所不同。 不過,從 .NET 7 開始,排序會根據元件中的中繼資料排序來確定。

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的巢狀類型:

這個方法只會傳回目前型別的巢狀類型。 它不會搜尋目前型別的基類。 若要尋找巢狀在基類中的類型,您必須逐步執行繼承階層,在每個層級呼叫 GetNestedTypes

BindingFlags.InstanceBindingFlags.Static 會被忽略。

BindingFlags.Public 使用 旗標或只有 BindingFlags.NonPublic 旗標呼叫這個方法會傳回指定的巢狀類型,而且不需要任何其他旗標。

如需相關資訊,請參閱 System.Reflection.BindingFlags

如果目前 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的巢狀類型。

如果巢狀類型為泛型,這個方法會傳回其泛型型別定義。 即使封入泛型型別是封閉式建構型別,也是如此。

注意

如果目前 Type 代表 C#、Visual Basic 或 C++ 中定義的泛型型別,即使它們本身沒有泛型參數,其巢狀類型也是全部泛型。 這不一定是動態元件中定義的巢狀類型,也不一定是使用 Ilasm.exe (IL 組合器編譯)

如需巢狀泛型型別的相關資訊,以及從其泛型型別定義建構巢狀泛型型別的詳細資訊,請參閱 MakeGenericType

另請參閱

適用於