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.

实现

示例

下面的示例定义了一个嵌套的类和一个structMyClass,然后获取嵌套的类型使用的类型的对象和MyClassThe 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
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

注解

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 NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终是按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件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 NoNo 可以。Yes. 方法 (包括虚拟和非虚拟) 可以隐藏通过名称或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套的类型Nested Type NoNo NoNo
PropertyProperty 不适用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. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。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 访问器,派生的类属性隐藏基类属性,并且你将无法再访问基类上的资源库。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表示泛型类型中定义C#,Visual Basic 中,或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. 这不一定是真正的动态程序集中定义的或使用编译的嵌套类型Ilasm.exe (IL 汇编程序)This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

在嵌套的泛型类型和构造从各自的泛型类型定义的嵌套泛型类型信息,请参阅MakeGenericTypeFor 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- 若为 Default,则返回 nullDefault 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表示泛型类型中定义C#,Visual Basic 中,或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. 这不一定是真正的动态程序集中定义的或使用编译的嵌套类型Ilasm.exe (IL 汇编程序)This is not necessarily true of nested types defined in dynamic assemblies or compiled with the Ilasm.exe (IL Assembler).

在嵌套的泛型类型和构造从各自的泛型类型定义的嵌套泛型类型信息,请参阅MakeGenericTypeFor information on nested generic types, and on constructing nested generic types from their generic type definitions, see MakeGenericType.

另请参阅

适用于