System.Type.GetFields 方法 (BindingFlags)

方法描述

当在派生类中重写时,使用指定绑定约束,搜索为当前 Type 定义的字段。

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

public abstract FieldInfo[] GetFields(
	BindingFlags bindingAttr
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
bindingAttr System-Reflection-BindingFlags 一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。- 或 -零,以返回 null。
返回值 System.Reflection.FieldInfo[] 表示为当前 Type 定义的匹配指定绑定约束的所有字段的 FieldInfo 对象数组。 - 或 - 如果没有为当前 Type 定义的字段,或者如果没有一个定义的字段匹配绑定约束,则为 FieldInfo 类型的空数组。

提示和注释

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

可以使用下列 BindingFlags 筛选标志定义包含在搜索中的字段:

为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。

指定 BindingFlags.Public 可在搜索中包含公共字段。

指定 BindingFlags.NonPublic 可在搜索中包含非公共字段(即私有字段、内部字段和受保护的字段)。 只返回基类上的受保护字段和内部字段;不返回基类上的私有字段。

指定 BindingFlags.FlattenHierarchy 以便沿层次结构向上包括 public 和 protected 静态成员;不包括继承类中的 private 静态成员。

下列 BindingFlags 修饰符标志可用于更改搜索的执行方式:

BindingFlags.DeclaredOnly ,表示仅搜索在 Type 上声明的字段,而不搜索简单继承的字段。

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

如果当前 Type 表示构造泛型类型,则此方法将返回 FieldInfo 对象,并且其类型形参替换为相应的类型实参。

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

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

下面的示例显示 GetFields(BindingFlags) 方法的用法。

using System;
using System.Reflection;

class AttributesSample
{
    public void Mymethod (int int1m, out string str2m, ref string str3m)
    {
        str2m = "in Mymethod";
    }

    public static int Main(string[] args)
    {      
        Console.WriteLine ("Reflection.MethodBase.Attributes Sample");

        // Get the type.
        Type MyType = Type.GetType("AttributesSample");

        // Get the method Mymethod on the type.
        MethodBase Mymethodbase = MyType.GetMethod("Mymethod");

        // Display the method name.
        Console.WriteLine("Mymethodbase = " + Mymethodbase);

        // Get the MethodAttribute enumerated value.
        MethodAttributes Myattributes = Mymethodbase.Attributes;

        // Display the flags that are set.
        PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
        return 0;
    }


    public static void PrintAttributes(Type attribType, int iAttribValue)
    {
        if (!attribType.IsEnum)
        { 
            Console.WriteLine("This type is not an enum."); 
            return; 
        }

        FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
        for (int i = 0; i < fields.Length; i++)
        {
            int fieldvalue = (Int32)fields[i].GetValue(null);
            if ((fieldvalue & iAttribValue) == fieldvalue)
            {
                Console.WriteLine(fields[i].Name);
            }
        }
    }
}

异常

异常 异常描述

命名空间

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 系统要求。