Export (0) Print
Expand All
Expand Minimize

FieldAttributes Enumeration

Specifies flags that describe the attributes of a field.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Reflection
Assemblies:   mscorlib (in mscorlib.dll)
  System.Reflection.Primitives (in System.Reflection.Primitives.dll)

[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum FieldAttributes

Member nameDescription
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsAssemblySpecifies that the field is accessible throughout the assembly.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFamANDAssemSpecifies that the field is accessible only by subtypes in this assembly.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFamilySpecifies that the field is accessible only by type and subtypes.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFamORAssemSpecifies that the field is accessible by subtypes anywhere, as well as throughout this assembly.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFieldAccessMaskSpecifies the access level of a given field.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsHasDefaultSpecifies that the field has a default value.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsHasFieldMarshalSpecifies that the field has marshaling information.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsHasFieldRVASpecifies 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.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsInitOnlySpecifies that the field is initialized only, and can be set only in the body of a constructor.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsLiteralSpecifies that the field's value is a compile-time (static or early bound) constant. Any attempt to set it throws a FieldAccessException.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNotSerializedSpecifies that the field does not have to be serialized when the type is remoted.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsPinvokeImplReserved for future use.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsPrivateSpecifies that the field is accessible only by the parent type.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsPrivateScopeSpecifies that the field cannot be referenced.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsPublicSpecifies that the field is accessible by any member for whom this scope is visible.
Supported by the XNA FrameworkReservedMaskReserved.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsRTSpecialNameSpecifies that the common language runtime (metadata internal APIs) should check the name encoding.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpecialNameSpecifies a special method, with the name describing how the method is special.
Supported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsStaticSpecifies that the field represents the defined type, or else it is per-instance.

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.

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.

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.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft