FieldAttributes 枚举

定义

指定描述字段属性的标志。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum FieldAttributes
C#
[System.Flags]
[System.Serializable]
public enum FieldAttributes
C#
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FieldAttributes
继承
FieldAttributes
属性

字段

名称 说明
Assembly 3

指定该字段可由整个程序集访问。

FamANDAssem 2

指定该字段只能由此程序集中的子类型访问。

Family 4

指定该字段只能由类型和子类型访问。

FamORAssem 5

指定该字段可由任意位置的子类型访问,也可由整个程序集访问。

FieldAccessMask 7

指定给定字段的访问级别。

HasDefault 32768

指定该字段具有默认值。

HasFieldMarshal 4096

指定该字段包含封送处理信息。

HasFieldRVA 256

指定该字段具有相对虚拟地址 (RVA)。 RVA 是方法体在当前图像中的位置,它是相对于它所在的图像文件的开始的地址。

InitOnly 32

指明该字段只能初始化,只可在构造函数的函数体中设置。

Literal 64

指定该字段的值是一个编译时(静态或早期绑定)常数。 设置它的任何试图将引出 FieldAccessException

NotSerialized 128

指定扩展类型时不必序列化该字段。

PinvokeImpl 8192

留待将来使用。

Private 1

指定该字段只能由父类型访问。

PrivateScope 0

指定该字段不能被引用。

Public 6

指定该字段可由任何可看见此范围的成员访问。

ReservedMask 38144

保留。

RTSpecialName 1024

指定公共语言运行时(元数据内部 API)应检查名称编码。

SpecialName 512

指定一个特殊方法,并用名称说明该方法的特殊性。

Static 16

指定该字段表示已定义的类型,否则为每实例方式。

示例

在此示例中,将生成三个字段并 FieldAttributes 显示值。 值 FieldAttributes 可以包含多个属性,例如 和 PublicLiteral,如第三个字段中所示。

C#
using System;
using System.Reflection;

public class Demo
{
    // Make three fields:
    // The first field is private.
    private string m_field = "String A";

    // The second field is public.
    public string Field = "String B";

    // The third field is public const (hence also literal and static),
    // with a default value.
    public const string FieldC = "String C";
}

public class Myfieldattributes
{
    public static void Main()
    {
        Console.WriteLine ("\nReflection.FieldAttributes");
        Demo d = new Demo();

        // Get a Type object for Demo, and a FieldInfo for each of
        // the three fields. Use the FieldInfo to display field
        // name, value for the Demo object in d, and attributes.
        //
        Type myType = typeof(Demo);
        FieldInfo fiPrivate = myType.GetField("m_field",
            BindingFlags.NonPublic | BindingFlags.Instance);
        DisplayField(d, fiPrivate);

        FieldInfo fiPublic = myType.GetField("Field",
            BindingFlags.Public | BindingFlags.Instance);
        DisplayField(d, fiPublic);

        FieldInfo fiConstant = myType.GetField("FieldC",
            BindingFlags.Public | BindingFlags.Static);
        DisplayField(d, fiConstant);
    }

    static void DisplayField(Object obj, FieldInfo f)
    {
        // Display the field name, value, and attributes.
        //
        Console.WriteLine("{0} = \"{1}\"; attributes: {2}",
            f.Name, f.GetValue(obj), f.Attributes);
    }
}

/* This code example produces the following output:

Reflection.FieldAttributes
m_field = "String A"; attributes: Private
Field = "String B"; attributes: Public
FieldC = "String C"; attributes: Public, Static, Literal, HasDefault
 */

注解

FieldAttributes 使用 的值仅 FieldAccessMask 屏蔽与辅助功能相关的属性值部分。 例如,以下代码确定是否 Attributes 设置了公共位。

C#
FieldInfo fi = obj.GetType().GetField("field1");

if ((fi.Attributes & FieldAttributes.FieldAccessMask) ==
    FieldAttributes.Public)
{
    Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj));
}

若要获取 , FieldAttributes请先获取 类 TypeType从 中获取 FieldInfoFieldInfo从 中获取 Attributes

枚举值是一个数字,表示在 字段上实现的属性的按位 OR。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0