Indicates whether the value is written at compile time and cannot be changed.
[Visual Basic] Public ReadOnly Property IsLiteral As Boolean [C#] public bool IsLiteral {get;} [C++] public: __property bool get_IsLiteral(); [JScript] public function get IsLiteral() : Boolean;
Read-only. true if the field has the Literal attribute set; otherwise, false.
The IsLiteral property is set when the FieldAttributes.Literal attribute is set. If this attribute is set, the field cannot be changed and is constant. Note in the sample below, the second field has IsLiteral set to true, the field is a constant, and the field has no set accessor.
[C#]
//Make two fields public class Myfielda { private string field = "A private field"; public string Field{ get{return field;} set{if(field!=value){field=value;}} } } public class Myfieldb { public const string field = "B public field"; public string Field{ get{return field;} } } public class Myfieldinfo { public static int Main() { Console.WriteLine("\nReflection.FieldInfo"); Myfielda Myfielda = new Myfielda(); Myfieldb Myfieldb = new Myfieldb(); //Get the Type and FieldInfo Type MyTypea = Type.GetType("Myfielda"); FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic); Type MyTypeb = Type.GetType("Myfieldb"); FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic); //For the first field; //Get and Display the Name, field, and IsLiteral Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0};", Myfieldinfoa.GetValue(Myfielda)); Console.Write(" IsLiteral = {0}; ", Myfieldinfoa.IsLiteral); if (Myfieldinfoa.IsLiteral) Console.Write("Field cannot be changed"); //For the second field; //Get and Display the Name, field, and IsLiteral Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); Console.Write(" IsLiteral = {0}; ", Myfieldinfob.IsLiteral); if (Myfieldinfob.IsLiteral) Console.Write("Field cannot be changed"); return 0; } }
This code produces the following output:
Reflection.FieldInfo
Myfielda- A private field; IsLiteral = False;
Myfieldb- B public field; IsLiteral = True; Field cannot be changed
FieldInfo Class | FieldInfo Members | System.Reflection Namespace | FieldAttributes