FieldAttributes Enumeration
TOC
Collapse the table of content
Expand the table of content

FieldAttributes Enumeration

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Specifies the attributes of a field.

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

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

[FlagsAttribute]
public enum FieldAttributes

Member nameDescription
AssemblyThe field is accessible throughout the assembly.
FamANDAssemThe field is accessible only by subtypes in this assembly.
FamilyThe field is accessible only by type and subtypes.
FamORAssemThe field is accessible by subtypes anywhere, as well as throughout this assembly.
FieldAccessMaskSpecifies the access level of a given field.
HasDefaultThe field has a default value.
HasFieldMarshalThe field has marshaling information.
HasFieldRVAThe 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.
InitOnlyThe field is initialized only, and cannot be written after initialization.
LiteralThe field's value is a compile-time (static or early bound) constant. Any attempt to set it throws FieldAccessException.
NotSerializedThe field does not have to be serialized when the type is remoted.
PinvokeImplReserved.
PrivateThe field is accessible only by the parent type.
PrivateScopeThe field cannot be referenced.
PublicThe field is accessible by any member for whom this scope is visible.
ReservedMaskReserved.
RTSpecialNameThe common language runtime (internal metadata APIs) should check the name encoding.
SpecialNameSpecifies a special field, with the name describing how the field is special.
StaticThe 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:

if ((Attributes & FieldAttributes.FieldAccessMask) == 
    FieldAttributes.Public)

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.

This example shows how to test for the enumeration members that describe access level.


using System;
using System.Reflection;

public class Example
{
   // 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";


   // Make the output TextBlock visible to all Shared members.
   static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      outputBlock.Text += "\nReflection.FieldAttributes\n";
      Example ex = new Example();

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

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

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

   static void DisplayField(Object obj, FieldInfo f)
   {
      object value = "<not accessible>";
      if (f.IsPublic)
      {
         value = f.GetValue(obj);
      }

      // Display the field name, value, and attributes.
      //
      outputBlock.Text += String.Format("{0} = \"{1}\"; attributes: {2}\n",
          f.Name, value.ToString(), f.Attributes);
   }
}

/* This code example produces the following output:

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft