System.Type.GetNestedTypes 方法 (BindingFlags)

方法描述

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

语法定义(C# System.Type.GetNestedTypes 方法 (BindingFlags) 的用法)

public abstract Type[] GetNestedTypes(
	BindingFlags bindingAttr
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
bindingAttr System-Reflection-BindingFlags 一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。- 或 -零,以返回 null。
返回值 System.Type[] Type 对象数组,这些对象表示嵌套在当前 Type 中的所有与指定的绑定约束匹配的类型(搜索是非递归的);如果没有找到与绑定约束匹配的嵌套类型,则为 Type 类型的空数组。

提示和注释

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

GetNestedTypes 方法不按特定的顺序(如字母顺序或声明顺序)返回类型。 您的代码一定不能依赖于类型的返回顺序,因为该顺序可以改变。

可以使用下列 BindingFlags 筛选标志定义包含在搜索中的嵌套类型:

要获取返回值,必须指定 BindingFlags.Public 或 BindingFlags.NonPublic。

指定 BindingFlags.Public 可在搜索中包含公共嵌套类型。

指定 BindingFlags.NonPublic 可在搜索中包含非公共嵌套类型(即私有类型、内部类型和受保护类型)。

此方法只返回当前类型的嵌套类型。 它不搜索当前类型的基类。 若要查找嵌套在基类中的类型,必须搜索继承层次结构,并在每一层都调用 GetNestedTypes。

忽略 BindingFlags.Instance 和 BindingFlags.Static。

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

有关更多信息,请参见 System.Reflection.BindingFlags。

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

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

注意

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

有关嵌套泛型类型以及基于泛型类型定义构造嵌套泛型类型的信息,请参见 MakeGenericType。

System.Type.GetNestedTypes 方法 (BindingFlags)例子

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

using System;
using System.Reflection;
using System.Reflection.Emit;

// Create a class with two nested public classes and two 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|BindingFlags.Instance);
        Console.WriteLine("The number of nested public classes is {0}.", myTypeArray.Length);
        // Display all the public nested classes.
        DisplayTypeInfo(myTypeArray);
        // 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.
        for(int i=0;i

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。