Indicates whether the field is static.
[Visual Basic] Public ReadOnly Property IsStatic As Boolean [C#] public bool IsStatic {get;} [C++] public: __property bool get_IsStatic(); [JScript] public function get IsStatic() : Boolean;
Read-only. true if this field is static; otherwise, false.
When a field is static, one copy of the field is shared by all instances of the class. A static field is also called a class variable.
The IsStatic property is set when the FieldAttributes.Static attribute is set.
To get the IsStatic property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsStatic property. To access a non public property, in the GetField method, set the BindingFlags to NonPublic.
[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 { static string field = "B static field"; public string Field { get{return field;} set{if(field!=value){field=value;}} } } 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 IsStatic Console.Write("\n{0} - ", MyTypea.FullName); Console.Write("{0}; ", Myfieldinfoa.GetValue(Myfielda)); Console.Write("IsStatic - {0}", Myfieldinfoa.IsStatic); //For the second field; //Get and Display the Name, field, and IsStatic Console.Write("\n{0} - ", MyTypeb.FullName); Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb)); Console.Write("IsStatic - {0}", Myfieldinfob.IsStatic); return 0; } }
This code produces the following output:
Reflection.FieldInfo
Myfielda- A private field; IsStatic- False
Myfieldb- B static field; IsStatic- True
FieldInfo Class | FieldInfo Members | System.Reflection Namespace | FieldAttributes