C# FieldInfo Attributes

Description

FieldInfo Attributes Gets the attributes associated with this field.

Syntax

FieldInfo.Attributes has the following syntax.


public abstract FieldAttributes Attributes { get; }

Example

Gets the attributes associated with this field.


using System;//from   ww w  .j av a 2 s .  c o  m
using System.Reflection;

public class Demo
{
    private string m_field = "String A";
    public string Field = "String B";
    public const string FieldC = "String C";
}

public class Myfieldattributes
{
    public static void Main()
    {
        Demo d = new Demo();
        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)
    { 
        Console.WriteLine(f.Attributes);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo