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
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration FieldAttributes
'Usage
Dim instance As FieldAttributes

/** @attribute SerializableAttribute() */ 
/** @attribute FlagsAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum FieldAttributes
SerializableAttribute 
FlagsAttribute 
ComVisibleAttribute(true) 
public enum FieldAttributes

 Member nameDescription
Supported by the .NET Compact FrameworkAssemblySpecifies that the field is accessible throughout the assembly. 
Supported by the .NET Compact FrameworkFamANDAssemSpecifies that the field is accessible only by subtypes in this assembly. 
Supported by the .NET Compact FrameworkFamilySpecifies that the field is accessible only by type and subtypes. 
Supported by the .NET Compact FrameworkFamORAssemSpecifies that the field is accessible by subtypes anywhere, as well as throughout this assembly. 
Supported by the .NET Compact FrameworkFieldAccessMaskSpecifies the access level of a given field. 
Supported by the .NET Compact FrameworkHasDefaultSpecifies that the field has a default value. 
Supported by the .NET Compact FrameworkHasFieldMarshalSpecifies that the field has marshalling information. 
Supported by the .NET Compact FrameworkHasFieldRVASpecifies 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 .NET Compact FrameworkInitOnlySpecifies that the field is initialized only, and cannot be written after initialization. 
Supported by the .NET Compact FrameworkLiteralSpecifies that the field's value is a compile-time (static or early bound) constant. No set accessor. 
Supported by the .NET Compact FrameworkNotSerializedSpecifies that the field does not have to be serialized when the type is remoted. 
Supported by the .NET Compact FrameworkPinvokeImplReserved for future use. 
Supported by the .NET Compact FrameworkPrivateSpecifies that the field is accessible only by the parent type. 
Supported by the .NET Compact FrameworkPrivateScopeSpecifies that the field cannot be referenced. 
Supported by the .NET Compact FrameworkPublicSpecifies that the field is accessible by any member for whom this scope is visible. 
Supported by the .NET Compact FrameworkReservedMaskReserved. 
Supported by the .NET Compact FrameworkRTSpecialNameSpecifies that the common language runtime (metadata internal APIs) should check the name encoding. 
Supported by the .NET Compact FrameworkSpecialNameSpecifies a special method, with the name describing how the method is special. 
Supported by the .NET Compact FrameworkStaticSpecifies 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 is 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.

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.

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: