Type.GetNestedTypes 方法

定义

获取嵌套在当前 Type中的类型。

重载

名称 说明
GetNestedTypes()

返回嵌套在当前 Type中的公共类型。

GetNestedTypes(BindingFlags)

在派生类中重写时,使用指定的绑定约束搜索嵌套在当前 Type中的类型。

GetNestedTypes()

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

返回嵌套在当前 Type中的公共类型。

public:
 cli::array <Type ^> ^ GetNestedTypes();
public:
 virtual cli::array <Type ^> ^ GetNestedTypes();
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public Type[] GetNestedTypes();
public Type[] GetNestedTypes();
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)>]
member this.GetNestedTypes : unit -> Type[]
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 in MyClass,然后使用 类型获取嵌套类型的 MyClass对象。

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 非静态
构造函数
领域 Yes. 字段始终是按名称和签名隐藏的。
事件 不適用 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。
方法 Yes. 方法(虚拟和非虚拟)可以按名称隐藏或按名称和签名隐藏。
嵌套类型
财产 不適用 常见的类型系统规则是继承与实现属性的方法相同。 反射将属性视为按名称和签名隐藏。 请参阅下面的说明 2。
  1. 按名称和签名隐藏会考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、sentinel 和非托管调用约定。 这是二进制比较。

  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
Source:
Type.cs
Source:
Type.cs

在派生类中重写时,使用指定的绑定约束搜索嵌套在当前 Type中的类型。

public:
 abstract cli::array <Type ^> ^ GetNestedTypes(System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicNestedTypes | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)]
public abstract Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr);
public abstract Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicNestedTypes | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicNestedTypes)>]
abstract member GetNestedTypes : System.Reflection.BindingFlags -> Type[]
abstract member GetNestedTypes : System.Reflection.BindingFlags -> Type[]
Public MustOverride Function GetNestedTypes (bindingAttr As BindingFlags) As Type()

参数

bindingAttr
BindingFlags

枚举值的按位组合,用于指定如何执行搜索。

-或-

Default null返回 。

返回

Type[]

一个 Type 对象数组,表示与指定的绑定约束(搜索不是递归的)或类型空数组(如果找不到与绑定约束匹配的嵌套类型)的当前 Type 中嵌套的所有类型 Type

实现

属性

示例

以下示例创建两个嵌套公共类和两个嵌套受保护的类,并显示与指定绑定约束匹配的类的信息。

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.Instance 将被 BindingFlags.Static 忽略。

BindingFlags.Public 使用标志或只有 BindingFlags.NonPublic 标志调用此方法将返回指定的嵌套类型,并且不需要任何其他标志。

有关详细信息,请参阅 System.Reflection.BindingFlags

如果当前 Type 表示泛型类型或泛型方法定义中的类型参数,此方法将搜索类约束的嵌套类型。

如果嵌套类型是泛型类型,此方法将返回其泛型类型定义。 即使封闭泛型类型是封闭构造类型,也是如此。

注释

如果当前 Type 表示在 C#、Visual Basic 或 C++ 中定义的泛型类型,则其嵌套类型都是泛型类型,即使它们没有自己的泛型参数也是如此。 这不一定是动态程序集中定义的嵌套类型,也不一定是使用 Ilasm.exe(IL 汇编程序)编译的嵌套类型。

有关嵌套泛型类型以及从其泛型类型定义构造嵌套泛型类型的信息,请参阅 MakeGenericType

另请参阅

适用于