Type.GetNestedTypes 方法

定义

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

重载

GetNestedTypes()

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

GetNestedTypes(BindingFlags)

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

GetNestedTypes()

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

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

C#
public Type[] GetNestedTypes();

返回

Type[]

Type 对象的数组,这些对象表示嵌套在当前 Type 中的公共类型(搜索是非递归的);如果当前的 Type 中没有嵌套公共类型,则为 Type 类型的空数组。

实现

示例

以下示例在 中MyClass定义嵌套类和 ,struct然后使用 类型MyClass获取嵌套类型的 对象。

C#
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);
        }
    }
}

注解

在 .NET 6 及更早版本中, GetNestedTypes 方法不按特定顺序(如字母顺序或声明顺序)返回类型。 代码不得依赖于返回类型的顺序,因为该顺序会有所不同。 但是,从 .NET 7 开始,根据程序集中的元数据排序,排序是确定性的。

仅返回立即嵌套在当前类型中的公共类型;搜索不是递归的。

下表显示了在对类型进行反射时,方法将返回 Get 基类的哪些成员。

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

  2. 为了反射,属性和事件是按名称和签名隐藏的。 如果基类中有一个同时具有 get 和 set 访问器的属性,但派生类只有一个 get 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类上的 setter。

  3. 自定义属性不是通用类型系统的一部分。

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

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

备注

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

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

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetNestedTypes(BindingFlags)

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

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

C#
public abstract Type[] GetNestedTypes(System.Reflection.BindingFlags bindingAttr);

参数

bindingAttr
BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。

- 或 -

若为 Default,则返回 null

返回

Type[]

Type 对象数组,这些对象表示嵌套在当前 Type 中的所有与指定的绑定约束匹配的类型(搜索是非递归的);如果没有找到与绑定约束匹配的嵌套类型,则为 Type 类型的空数组。

实现

示例

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

C#
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.

注解

对嵌套类型的搜索不是递归的。

在 .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

另请参阅

适用于

.NET 10 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1