Export (0) Print
Expand All
Expand Minimize

FieldAttributes Enumeration

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)

'Declaration
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration FieldAttributes

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

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 And FieldAttributes.FieldAccessMask) = _
    FieldAttributes.Public Then

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.


Imports System.Reflection

Public Class Example
   ' 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"


   ' Make the output TextBlock visible to all Shared members.
   Private Shared outputBlock As System.Windows.Controls.TextBlock

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      ' Create an instance of the Example class.
      Dim ex As New Example()

      outputBlock.Text &= vbCrLf & "Reflection.FieldAttributes" & vbCrLf

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

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

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

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

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

      Dim value As Object = "<not accessible>"
      If f.IsPublic Then
         value = f.GetValue(obj)
      End If

      ' Display the field name, value, and attributes.
      '
      outputBlock.Text &= String.Format("{0} = ""{1}""; attributes: {2}", _
          f.Name, value.ToString(), f.Attributes) & vbCrLf
   End Sub

End Class

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


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft