Edit

Share via


FieldAttributes Enum

Definition

Specifies flags that describe the attributes of a field.

This enumeration supports a bitwise combination of its member values.

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
Inheritance
FieldAttributes
Attributes

Fields

Name Value Description
PrivateScope 0

Specifies that the field cannot be referenced.

Private 1

Specifies that the field is accessible only by the parent type.

FamANDAssem 2

Specifies that the field is accessible only by subtypes in this assembly.

Assembly 3

Specifies that the field is accessible throughout the 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.

Public 6

Specifies that the field is accessible by any member for whom this scope is visible.

FieldAccessMask 7

Specifies the access level of a given field.

Static 16

Specifies that the field represents the defined type, or else it is per-instance.

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. Any attempt to set it throws a FieldAccessException.

NotSerialized 128

Specifies that the field does not have to be serialized when the type is remoted.

HasFieldRVA 256

Specifies that the field has a relative virtual address (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.

SpecialName 512

Specifies a special method, with the name describing how the method is special.

RTSpecialName 1024

Specifies that the common language runtime (metadata internal APIs) should check the name encoding.

HasFieldMarshal 4096

Specifies that the field has marshaling information.

PinvokeImpl 8192

Reserved for future use.

HasDefault 32768

Specifies that the field has a default value.

ReservedMask 38144

Reserved.

Examples

In this example, three fields are built and the FieldAttributes values are displayed. A FieldAttributes value can contain more than one attribute, for example, both Public and Literal, as shown in the third field.

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
 */

Remarks

FieldAttributes uses the value from FieldAccessMask to mask off only the parts of the attribute value that pertain to the accessibility. For example, the following code determines if Attributes has the public bit set.

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

To get the FieldAttributes, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the Attributes.

The enumerated value is a number representing the bitwise OR of the attributes implemented on the field.

Applies to

Product Versions
.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