System.Reflection.FieldInfo.Attributes Property

Gets the attributes associated with this field.

Syntax

public abstract FieldAttributes Attributes { get; }

Value

A System.Reflection.FieldAttributes value that indicates the attributes of the field reflected by the current instance.

Remarks

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.

Example

The following example demonstrates obtaining the attributes of two fields.

C# Example

using System;
using System.Reflection;

class MyClass
{

   public int MyPublicInstanceField;
   private const int MyPrivateConstField = 10;

}

class FieldAttributesExample
{

   public static void Main()
   {

      Type t = (typeof(MyClass));
      string str;
      FieldInfo[] fiAry = t.GetFields( BindingFlags.Static |
         BindingFlags.Instance | BindingFlags.Public |
         BindingFlags.NonPublic | BindingFlags.DeclaredOnly );
      foreach (FieldInfo fi in fiAry)
      {
         Console.WriteLine("Field {0} is: ", fi.Name);
         str = ((fi.Attributes & FieldAttributes.Static) != 0) ?
            "Static" : "Instance";
         Console.Write(str + " ");
         str = ((fi.Attributes & FieldAttributes.Public) != 0) ?
            "Public" : "Not-Public";
         Console.Write(str + " ");
         str = ((fi.Attributes & FieldAttributes.Literal) != 0) ?
            "Literal" : String.Empty;
         Console.WriteLine(str);

      }

   }

}
      

The output is

Field MyPublicInstanceField is:
Instance Public
Field MyPrivateConstField is:
Static Not-Public Literal

Requirements

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0