Type.GetNestedTypes 方法

定义

获取嵌套在当前 Type 中的类型。Gets the types nested within the current Type.

重载

GetNestedTypes()

返回嵌套在当前的 Type 中的公共类型。Returns the public types nested in the current Type.

GetNestedTypes(BindingFlags)

当在派生类中重写时,使用指定绑定约束搜索嵌套在当前 Type 中的类型。When overridden in a derived class, searches for the types nested in the current Type, using the specified binding constraints.

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.

实现

示例

下面的示例在 MyClass中定义了嵌套类和 struct,然后使用 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

    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

注解

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
属性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. 按名称和签名隐藏将考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、个 sentinel 和非托管调用约定。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 (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)

当在派生类中重写时,使用指定绑定约束搜索嵌套在当前 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

枚举值的按位组合,这些值指定如何进行搜索。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. 若要查找嵌套在基类中的类型,必须遍历继承层次结构,并在每个级别调用 GetNestedTypesTo 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 (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.

另请参阅

适用于