Export (0) Print
Expand All

FieldInfo.Attributes Property

Gets the attributes associated with this field.

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

public abstract FieldAttributes Attributes { get; }

Property Value

Type: System.Reflection.FieldAttributes
The FieldAttributes for this field.

Implements

_FieldInfo.Attributes

All members have a set of attributes, which are defined in relation to the specific type of member. FieldAttributes informs the user whether this field is the private field, a static field, and so on.

To get the Attributes property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the Attributes.

The following code example builds three fields and displays their field attributes. A FieldAttributes value can contain more than one attribute, such as 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.5.2, 4.5.1, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

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