Type.GetNestedTypes Type.GetNestedTypes Type.GetNestedTypes Type.GetNestedTypes Method

定義

讓其在目前的 Type 內變成巢狀之類型。Gets the types nested within the current Type.

多載

GetNestedTypes() GetNestedTypes() GetNestedTypes() GetNestedTypes()

傳回在目前 Type 內形成巢狀的公用類型。Returns the public types nested in the current Type.

GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋在目前 Type 內形成巢狀的類型。When overridden in a derived class, searches for the types nested in the current Type, using the specified binding constraints.

GetNestedTypes() GetNestedTypes() GetNestedTypes() GetNestedTypes()

傳回在目前 Type 內形成巢狀的公用類型。Returns the public types nested in the current Type.

public:
 virtual cli::array <Type ^> ^ GetNestedTypes();
public Type[] GetNestedTypes ();
abstract member GetNestedTypes : unit -> Type[]
override this.GetNestedTypes : unit -> Type[]
Public Function GetNestedTypes () As Type()

傳回

Type[]

Type 物件的陣列,代表目前 Type 中的巢狀公用類型 (搜尋不是遞迴的);如果目前 Type 中沒有巢狀公用類型,則為 Type 類型的空陣列。An array of Type objects representing the public types nested in the current Type (the search is not recursive), or an empty array of type Type if no public types are nested in the current Type.

實作

範例

下列範例會struct在中MyClass定義一個嵌套類別和,然後使用類型來MyClass取得巢狀型別的物件。The following example defines a nested class and a struct in MyClass, and then obtains objects of the nested types using the type of 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 '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
End Class 'MyMainClass

備註

GetNestedTypes方法不會以特定順序傳回類型,例如依字母順序或宣告順序。The GetNestedTypes method does not return types in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回類型的順序,因為該順序會有所不同。Your code must not depend on the order in which types are returned, because that order varies.

只會傳回立即嵌套在目前類型中的公用類型;搜尋不是遞迴的。Only the public types immediately nested in the current type are returned; the search is not recursive.

下表顯示當在類型上反映時, Get方法所傳回之基類的成員。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位一律會依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
方法Method No 可以。Yes. 方法(虛擬和非虛擬)可以依名稱或逐名稱和簽章來隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀型別Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是,繼承與實作為屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會將屬性視為逐名稱和簽章。Reflection treats properties as hide-by-name-and-signature. 請參閱下面的注2。See note 2 below.
  1. [逐名稱] 和 [簽章] 會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、sentinels 和非受控呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 針對反映,屬性和事件是依名稱和簽章來隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您在基類中具有具有 get 和 set 存取子的屬性,但衍生類別只有 get 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不是一般型別系統的一部分。Custom attributes are not part of the common type system.

如果目前Type的表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的巢狀型別。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the nested types of the class constraint.

如果嵌套的類型是泛型,則這個方法會傳回它的泛型型別定義。If a nested type is generic, this method returns its generic type definition. 即使封閉式泛型型別是封閉的結構化型別,也是如此。This is true even if the enclosing generic type is a closed constructed type.

注意

如果目前Type的表示在、Visual Basic 或C++中C#定義的泛型型別,則其巢狀型別會全部都是泛型,即使它們本身沒有泛型參數也一樣。If the current Type represents a generic type defined in C#, Visual Basic, or C++, its nested types are all generic even if they have no generic parameters of their own. 這不一定適用于動態元件中定義的巢狀型別,或以eseutil.exe (IL組合器)編譯的。This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

如需有關嵌套泛型型別,以及從其泛型型別定義來建立嵌套泛型型別的詳細MakeGenericType資訊,請參閱。For information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

另請參閱

GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags) GetNestedTypes(BindingFlags)

在衍生類別中覆寫時,使用指定的繫結條件約束,搜尋在目前 Type 內形成巢狀的類型。When overridden in a derived class, searches for the types nested in the current Type, using the specified binding constraints.

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 BindingFlags BindingFlags BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

傳回

Type[]

Type 物件的陣列,代表目前 Type 中符合指定繫結條件約束的所有巢狀類型 (搜尋不是遞迴的);如果找不到符合繫結條件約束的巢狀類型,則為 Type 類型的空陣列。An array of Type objects representing all the types nested in the current Type that match the specified binding constraints (the search is not recursive), or an empty array of type Type, if no nested types are found that match the binding constraints.

實作

範例

下列範例會建立兩個嵌套的公用類別和兩個嵌套的受保護類別,並顯示符合指定系結條件約束之類別的資訊。The following example creates two nested public classes and two nested protected classes, and displays information for classes that match the specified binding constraints.

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.

備註

巢狀型別的搜尋不是遞迴的。The search for nested types is not recursive.

GetNestedTypes方法不會以特定順序傳回類型,例如依字母順序或宣告順序。The GetNestedTypes method does not return types in a particular order, such as alphabetical or declaration order. 您的程式碼不能相依于傳回類型的順序,因為該順序會有所不同。Your code must not depend on the order in which types are returned, because that order varies.

下列BindingFlags篩選旗標可以用來定義要包含在搜尋中的巢狀型別:The following BindingFlags filter flags can be used to define which nested types to include in the search:

這個方法只會傳回目前類型的巢狀型別。This method returns only the nested types of the current type. 它不會搜尋目前類型的基類。It does not search the base classes of the current type. 若要尋找內嵌于基類中的類型,您必須逐步進行繼承階層,並GetNestedTypes在每個層級呼叫。To find types that are nested in base classes, you must walk the inheritance hierarchy, calling GetNestedTypes at each level.

BindingFlags.InstanceBindingFlags.Static會被忽略。BindingFlags.Instance and BindingFlags.Static are ignored.

只要BindingFlags.Public使用旗標或BindingFlags.NonPublic只有旗標呼叫此方法,就會傳回指定的巢狀型別,而且不需要任何其他旗標。Calling this method with only the BindingFlags.Public flag or only the BindingFlags.NonPublic flag will return the specified nested types and does not require any other flags.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果目前Type的表示泛型型別或泛型方法定義中的類型參數,這個方法會搜尋類別條件約束的巢狀型別。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the nested types of the class constraint.

如果嵌套的類型是泛型,則這個方法會傳回它的泛型型別定義。If a nested type is generic, this method returns its generic type definition. 即使封閉式泛型型別是封閉的結構化型別,也是如此。This is true even if the enclosing generic type is a closed constructed type.

注意

如果目前Type的表示在、Visual Basic 或C++中C#定義的泛型型別,則其巢狀型別會全部都是泛型,即使它們本身沒有泛型參數也一樣。If the current Type represents a generic type defined in C#, Visual Basic, or C++, its nested types are all generic even if they have no generic parameters of their own. 這不一定適用于動態元件中定義的巢狀型別,或以eseutil.exe (IL組合器)編譯的。This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

如需有關嵌套泛型型別,以及從其泛型型別定義來建立嵌套泛型型別的詳細MakeGenericType資訊,請參閱。For information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

另請參閱

適用於