FieldInfo.IsInitOnly Property indicates whether the field can only be set in the body of the constructor.
using System;
using System.Reflection;
public class Myfielda
{
public string field = "A - public modifiable field";
}
public class Myfieldb
{
public readonly string field = "B - readonly field";
}
public class Myfieldinfo
{
public static int Main()
{
Myfielda Myfielda = new Myfielda();
Myfieldb Myfieldb = new Myfieldb();
Type MyTypea = typeof(Myfielda);
FieldInfo Myfieldinfoa = MyTypea.GetField("field",BindingFlags.Public | BindingFlags.Instance);
Type MyTypeb = typeof(Myfieldb);
FieldInfo Myfieldinfob = MyTypeb.GetField("field",BindingFlags.Public | BindingFlags.Instance);
Myfielda.field = "A - modified";
Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
MyTypea.FullName,
Myfieldinfoa.GetValue(Myfielda),
Myfieldinfoa.IsInitOnly);
Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
MyTypeb.FullName,
Myfieldinfob.GetValue(Myfieldb),
Myfieldinfob.IsInitOnly);
return 0;
}
}
Related examples in the same category