Get the attributes associated with this field in CSharp
Description
The following code shows how to get the attributes associated with this field.
Example
using System;//www .jav a 2s .c om
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.