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.

[Visual Basic]
<Flags>
<Serializable>
Public Enum FieldAttributes
[C#]
[Flags]
[Serializable]
public enum FieldAttributes
[C++]
[Flags]
[Serializable]
__value public enum FieldAttributes
[JScript]
public
   Flags
 Serializable
enum FieldAttributes

Remarks

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:

(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.

Members

Member name Description Value
Assembly

Supported by the .NET Compact Framework.

Specifies that the field is accessible throughout the assembly. 3
FamANDAssem

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

Specifies that the field is accessible only by type and subtypes. 4
FamORAssem

Supported by the .NET Compact Framework.

Specifies that the field is accessible by subtypes anywhere, as well as throughout this assembly. 5
FieldAccessMask

Supported by the .NET Compact Framework.

Specifies the access level of a given field. 7
HasDefault

Supported by the .NET Compact Framework.

Specifies that the field has a default value. 32768
HasFieldMarshal

Supported by the .NET Compact Framework.

Specifies that the field has marshalling information. 4096
HasFieldRVA

Supported by the .NET Compact Framework.

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. 256
InitOnly

Supported by the .NET Compact Framework.

Specifies that the field is initialized only, and cannot be written after initialization. 32
Literal

Supported by the .NET Compact Framework.

Specifies that the field's value is a compile-time (static or early bound) constant. No set accessor. 64
NotSerialized

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

Reserved for future use. 8192
Private

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

Specifies that the field cannot be referenced. 0
Public

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

Reserved. 38144
RTSpecialName

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

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

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#] In this example, three fields are built and the FieldAttributes value is displayed when it is exactly defined. A FieldAttributes may contain more than one attribute, such as both Public and Literal, as shown in the third field below.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Module Module1
    Public Class Myfieldattributes
        Public Shared Sub Main()
            Console.WriteLine(ControlChars.Cr + "Reflection.FieldAttributes")
            Dim Myfieldx As New Myfielda()
            Dim Myfieldy As New Myfieldb()
            Dim Myfieldz As New Myfieldc()

            'Get the Type and FieldInfo for each of the three fields.
            Dim MyTypea As Type
'Use GetType on an instance of a type instead of calling Type.GetType.
            MyTypea = Myfieldx.GetType()
            Dim Myfieldinfoa As FieldInfo = MyTypea.GetField("m_field", _
               BindingFlags.NonPublic Or BindingFlags.Instance)
            Dim MyTypeb As Type = Myfieldy.GetType()
            Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("m_field", _
               BindingFlags.Public Or BindingFlags.Instance)
            Dim MyTypec As Type = Myfieldz.GetType()
            Dim Myfieldinfoc As FieldInfo = MyTypec.GetField("m_field", _
               BindingFlags.Public Or BindingFlags.Static)

            'For the first field,
            'get and display the Name, m_field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypea.Name)
            Console.Write("{0}; ", Myfieldinfoa.GetValue(Myfieldx))
            Dim Myattributesa As FieldAttributes = Myfieldinfoa.Attributes

            'If the FieldAttributes are exactly defined,
            'display them. Otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesa) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesa.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If

            'For the second field,
            'get and display the Name, field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypeb.Name)
            Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldy))
            Dim Myattributesb As FieldAttributes = Myfieldinfob.Attributes

            'If the FieldAttributes are exactly defined,
            'display them; otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesb) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesb.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If

            'For the third field,
            'get and display the Name, field, and attributes.
            Console.Write(ControlChars.CrLf + "{0} - ", MyTypec.Name)
            Console.Write("{0}; ", Myfieldinfoc.GetValue(Myfieldz))
            Dim Myattributesc As FieldAttributes = Myfieldinfoc.Attributes

            'If the FieldAttributes are exactly defined,
            'display them; otherwise, describe as not defined.
            If [Enum].IsDefined(GetType(FieldAttributes), Myattributesc) Then
                Console.Write("it has a {0} field attribute.", _
                   Myattributesc.ToString())
            Else
                Console.Write("it is not exactly defined.")
            End If
        End Sub
    End Class
    'Make three fields
    'The first field is private
    Public Class Myfielda
        Private m_field As String = "A private field"

        Public Property Field() As String
            Get
                Return m_field
            End Get
            Set(ByVal Value As String)
                If m_field <> Value Then
                    m_field = Value + "---"
                End If
            End Set
        End Property
    End Class

    'The second field is public.
    Public Class Myfieldb
        Public m_field As String = "B public field"

        Public Property Field() As String
            Get
                Return m_field
            End Get
            Set(ByVal Value As String)
                If m_field <> Value Then
                    m_field = Value
                End If
            End Set
        End Property
    End Class

    'The third field is public and literal, which is not exactly defined.
    Public Class Myfieldc
        Public Const m_field As String = "C constant field"

        Public ReadOnly Property Field() As String
            Get
                Return m_field
            End Get
        End Property
    End Class

End Module

[C#] 
using System;
using System.Reflection;

//Make three fields
//The first field is private
public class Myfielda
{
    private string field = "A private field";
    public string Field{
        get{return field;}
        set{if(field!=value) {field=value + "---";}}
    }
}
//The second field is public
public class Myfieldb
{
    public string field = "B public field";
    public string Field{
        get{return field;}
        set{if(field!=value) {field=value;}}
    }
}
//The third field is public and literal, which is not exactly defined.
public class Myfieldc
{
    public const string field = "C constant field";
    public string Field{
        get{return field;}
    }
}
public class Myfieldattributes
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.FieldAttributes");
        Myfielda Myfielda = new Myfielda();
        Myfieldb Myfieldb = new Myfieldb();
        Myfieldc Myfieldc = new Myfieldc();

        //Get the Type and FieldInfo for each of the three fields
        Type MyTypea = Type.GetType("Myfielda");
        FieldInfo Myfieldinfoa = MyTypea.GetField("field",
            BindingFlags.NonPublic | BindingFlags.Instance);
        Type MyTypeb = Type.GetType("Myfieldb");
        FieldInfo Myfieldinfob = MyTypeb.GetField("field",
            BindingFlags.Public | BindingFlags.Instance);
        Type MyTypec = Type.GetType("Myfieldc");
        FieldInfo Myfieldinfoc = MyTypec.GetField("field",
            BindingFlags.Public | BindingFlags.Static);

        //For the first field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypea.FullName);
        Console.Write ("{0}; ", Myfieldinfoa.GetValue(Myfielda));
        FieldAttributes Myattributesa = Myfieldinfoa.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesa))
            Console.Write ("it has a {0} field attribute.",
                Myattributesa.ToString());
        else
            Console.Write ("it is not exactly defined.");

        //For the second field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypeb.FullName);
        Console.Write ("{0}; ", Myfieldinfob.GetValue(Myfieldb));
        FieldAttributes Myattributesb = Myfieldinfob.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesb))
            Console.Write ("it has a {0} field attribute.",
                Myattributesb.ToString());
        else
            Console.Write ("it is not exactly defined.");

        //For the third field;
        //Get and Display the Name, field, and attributes
        Console.Write ("\n{0} - ", MyTypec.FullName);
        Console.Write ("{0}; ", Myfieldinfoc.GetValue(Myfieldc));
        FieldAttributes Myattributesc = Myfieldinfoc.Attributes;

        //If the FieldAttributes is exactly defined,
        // print it out, otherwise say it is not defined
        if (Enum.IsDefined(typeof(FieldAttributes),
            Myattributesc))
            Console.Write ("it has a {0} field attribute.",
                Myattributesc.ToString());
        else
            Console.Write ("it is not exactly defined.");

        return 0;
    }
}

[Visual Basic, C#] This code produces the following output:

[Visual Basic, C#] Reflection.FieldAttributes

[Visual Basic, C#] Myfielda - A private field; it has a Private field attribute.

[Visual Basic, C#] Myfieldb - B public field; it has a Public field attribute.

[Visual Basic, C#] Myfieldc - C constant field; it is not exactly defined.

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Reflection Namespace

Show:
© 2014 Microsoft