FieldAttributes 枚举

定义

指定描述字段特性的标志。Specifies flags that describe the attributes of a field.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class FieldAttributes
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum FieldAttributes
type FieldAttributes = 
Public Enum FieldAttributes
继承
FieldAttributes
属性

字段

Assembly 3

指定该字段可由整个程序集访问。Specifies that the field is accessible throughout the assembly.

FamANDAssem 2

指定该字段只能由此程序集中的子类型访问。Specifies that the field is accessible only by subtypes in this assembly.

Family 4

指定该字段只能由类型和子类型访问。Specifies that the field is accessible only by type and subtypes.

FamORAssem 5

指定该字段可由任意位置的子类型访问,也可由整个程序集访问。Specifies that the field is accessible by subtypes anywhere, as well as throughout this assembly.

FieldAccessMask 7

指定给定字段的访问级别。Specifies the access level of a given field.

HasDefault 32768

指定该字段具有默认值。Specifies that the field has a default value.

HasFieldMarshal 4096

指定该字段包含封送处理信息。Specifies that the field has marshaling information.

HasFieldRVA 256

指定该字段具有相对虚拟地址 (RVA)。Specifies that the field has a relative virtual address (RVA). RVA 是方法体在当前图像中的位置,它是相对于它所在的图像文件的开始的地址。The RVA is the location of the method body in the current image, as an address relative to the start of the image file in which it is located.

InitOnly 32

指明该字段只能初始化,只可在构造函数的函数体中设置。Specifies that the field is initialized only, and can be set only in the body of a constructor.

Literal 64

指定该字段的值是一个编译时(静态或早期绑定)常数。Specifies that the field's value is a compile-time (static or early bound) constant. 设置它的任何试图将引出 FieldAccessExceptionAny attempt to set it throws a FieldAccessException.

NotSerialized 128

指定扩展类型时不必序列化该字段。Specifies that the field does not have to be serialized when the type is remoted.

PinvokeImpl 8192

留待将来使用。Reserved for future use.

Private 1

指定该字段只能由父类型访问。Specifies that the field is accessible only by the parent type.

PrivateScope 0

指定该字段不能被引用。Specifies that the field cannot be referenced.

Public 6

指定该字段可由任何可看见此范围的成员访问。Specifies that the field is accessible by any member for whom this scope is visible.

ReservedMask 38144

保留。Reserved.

RTSpecialName 1024

指定公共语言运行时(元数据内部 API)应检查名称编码。Specifies that the common language runtime (metadata internal APIs) should check the name encoding.

SpecialName 512

指定一个特殊方法,并用名称说明该方法的特殊性。Specifies a special method, with the name describing how the method is special.

Static 16

指定该字段表示已定义的类型,否则为每实例方式。Specifies that the field represents the defined type, or else it is per-instance.

示例

在此示例中, 生成三个字段并FieldAttributes显示值。In this example, three fields are built and the FieldAttributes values are displayed. 值可以包含多个属性, 例如PublicLiteral, 如第三个字段中所示。 FieldAttributesA FieldAttributes value can contain more than one attribute, for example, both Public and Literal, as shown in the third field.

using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;

public ref class Demo
{
private:
    // Make three fields:
    // The first field is private.
    String^ m_field;

    // The second field is public.
public:
    String^ Field;

    // The third field is public and literal. 
    literal String^ FieldC = "String C";

    Demo() { m_field = "String A"; Field = "String B"; }
};

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

void main()
{
    Console::WriteLine ("\nReflection.FieldAttributes");
    Demo^ d = gcnew 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 = Demo::typeid;

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

/* 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
 */
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
 */
Imports System.Reflection

Public Class Demo
    ' Declare three fields.
    ' The first field is private.
    Private m_field As String = "String A"

    'The second field is public.
    Public Field As String = "String B"

    ' The third field is public and const, hence also static
    ' and literal with a default value.
    Public Const FieldC As String = "String C"

End Class

Module Module1
    Sub Main()
        ' Create an instance of the Demo class.
        Dim d As New Demo()

        Console.WriteLine(vbCrLf & "Reflection.FieldAttributes")

        ' 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.
        '
        Dim myType As Type = GetType(Demo)

        Dim fiPrivate As FieldInfo = myType.GetField("m_field", _
            BindingFlags.NonPublic Or BindingFlags.Instance)
        DisplayField(d, fiPrivate)

        Dim fiPublic As FieldInfo = myType.GetField("Field", _
            BindingFlags.Public Or BindingFlags.Instance)
        DisplayField(d, fiPublic)

        Dim fiConstant As FieldInfo = myType.GetField("FieldC", _
            BindingFlags.Public Or BindingFlags.Static)
        DisplayField(d, fiConstant)
    End Sub

    Sub DisplayField(ByVal obj As Object, ByVal f As FieldInfo)

        ' Display the field name, value, and attributes.
        '
        Console.WriteLine("{0} = ""{1}""; attributes: {2}", _
            f.Name, f.GetValue(obj), f.Attributes)
    End Sub

End Module

' This code example produces the following output:
'
'm_field = "String A"; attributes: Private
'Field = "String B"; attributes: Public
'FieldC = "String C"; attributes: Public, Static, Literal, HasDefault

注解

FieldAttributes使用中FieldAccessMask的值仅屏蔽与可访问性相关的属性值部分。FieldAttributes uses the value from FieldAccessMask to mask off only the parts of the attribute value that pertain to the accessibility. 例如, 以下代码确定Attributes是否设置了公共位。For example, the following code determines if Attributes has the public bit set.

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));
}
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));
}
Dim fi As FieldInfo = obj.GetType().GetField("field1")

If (fi.Attributes And FieldAttributes.FieldAccessMask) = _
    FieldAttributes.Public Then
    Console.WriteLine("{0:s} is public. Value: {1:d}", fi.Name, fi.GetValue(obj))
End If

若要获取FieldAttributes, 请首先获取类TypeTo get the FieldAttributes, first get the class Type. Type从中FieldInfo获取。From the Type, get the FieldInfo. FieldInfo从中Attributes获取。From the FieldInfo, get the Attributes.

枚举值是一个数字, 表示对该字段实现的属性的按位 "或"。The enumerated value is a number representing the bitwise OR of the attributes implemented on the field.

适用于