FieldInfo.Attributes Property gets the attributes associated with this field.
using System;
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("{0} = \"{1}\"; attributes: {2}", f.Name, f.GetValue(obj), f.Attributes);
}
}
Related examples in the same category