This documentation is archived and is not being maintained.

FieldInfo.IsStatic Property

Gets a value indicating 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;

Property Value

true if this field is static; otherwise, false.

Remarks

When a field is static, one copy of the field is shared by all instances of the type.

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 field, set the BindingFlags to NonPublic in the GetField method and set the accessibility to Instance or Static.

Example

The following example determines whether the specified field is static and displays the result.

[Visual Basic] 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' Make two fields.
Public Class Myfielda
    Private m_field As String = "A private field"

    Public Property Field() As String
        Get
            Return m_field
        End Get
        Set(ByVal Value As String)
            If m_field <> value Then
                m_field = value
            End If
        End Set
    End Property
End Class

Public Class Myfieldb
    Private Shared m_field As String = "B static field"

    Public Property Field() As String
        Get
            Return m_field
        End Get
        Set(ByVal Value As String)
            If m_field <> value Then
                m_field = value
            End If
        End Set
    End Property
End Class

Public Class Myfieldinfo

    Public Shared Sub Main()
        Console.WriteLine()
        Console.WriteLine("Reflection.FieldInfo")
        Console.WriteLine()
        Dim Myfielda As New Myfielda()
        Dim Myfieldb As New Myfieldb()

        ' Get the Type and FieldInfo.
        Dim MyTypea As Type = Type.GetType("Myfielda")
        Dim Myfieldinfoa As FieldInfo = _
           MyTypea.GetField("m_field", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim MyTypeb As Type = Type.GetType("Myfieldb")
        Dim Myfieldinfob As FieldInfo = _
           MyTypeb.GetField("m_field", BindingFlags.NonPublic Or BindingFlags.Static)

        ' For the first field, get and display the name, field, and IsStatic property value.
        Console.WriteLine("{0} - {1}; IsStatic - {2}", MyTypea.FullName, Myfieldinfoa.GetValue(Myfielda), Myfieldinfoa.IsStatic)

        ' For the second field, get and display the name, field, and IsStatic property value.
        Console.WriteLine("{0} - {1}; IsStatic - {2}", MyTypeb.FullName, Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsStatic)

    End Sub
End Class

[C#] 
using System;
using System.Reflection;

 // 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|BindingFlags.Instance);
        Type MyTypeb = Type.GetType("Myfieldb");
        FieldInfo Myfieldinfob = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Static);
  
        // For the first field, get and display the name, field, and IsStatic property value.
        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 property value.
        Console.Write("\n{0} - ", MyTypeb.FullName);
        Console.Write("{0}; ", Myfieldinfob.GetValue(Myfieldb));
        Console.Write("IsStatic - {0}", Myfieldinfob.IsStatic);
  
        return 0;
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

// Make two fields.
public __gc class Myfielda
{
private:
    String* field;
public:
    Myfielda() : field(S"A private field") {}
    __property String* get_Field() {
        return field;
    }
    __property void set_Field(String* value) {
        if(field!=value){
            field=value;
        }
    }
};
public __gc class Myfieldb
{
    static String* field = S"B static field";
public:
    __property String* get_Field() {
        return field;
    }
    __property void set_Field(String* value) {
        if(field!=value){
            field=value;
        }
    }
};

int main()
{
    Console::WriteLine(S"\nReflection.FieldInfo");
    Myfielda* myfielda = new Myfielda();
    Myfieldb* myfieldb = new Myfieldb();

    // Get the Type and FieldInfo.
    Type* MyTypea = Type::GetType(S"Myfielda");
    FieldInfo* Myfieldinfoa = MyTypea->GetField(S"field",
        static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Instance));
    Type* MyTypeb = Type::GetType(S"Myfieldb");
    FieldInfo* Myfieldinfob = MyTypeb->GetField(S"field",
        static_cast<BindingFlags>(BindingFlags::NonPublic|BindingFlags::Static));

    // For the first field, get and display the name, field, and IsStatic property value.
    Console::Write(S"\n{0} - ", MyTypea->FullName);
    Console::Write(S"{0}; ", Myfieldinfoa->GetValue(myfielda));
    Console::Write(S"IsStatic - {0}", __box(Myfieldinfoa->IsStatic));

    // For the second field get and display the name, field, and IsStatic property value.
    Console::Write(S"\n{0} - ", MyTypeb->FullName);
    Console::Write(S"{0}; ", Myfieldinfob->GetValue(myfieldb));
    Console::Write(S"IsStatic - {0}", __box(Myfieldinfob->IsStatic));

    return 0;
}

[JScript] 
//Make two fields.
public class Myfielda
{
   private var field : String = "A private field";
   public function get Field() : String {
       return field;
   }
   
   public function set Field(value : String) {
       if(field!=value)field=value;
   }
}
public class Myfieldb
{
   private static var field : String = "B static field";
   public static function get Field() : String  {
       return field;
   }
   public static function set Field(value : String) {
       if(field!=value) field=value;
   }
}
 
public class Myfieldinfo
{
   public static function Main() : void
   {
      Console.WriteLine("\nReflection.FieldInfo");
      var myfielda : Myfielda = new Myfielda();
      var myfieldb : Myfieldb = new Myfieldb();
 
      //Get the Type and FieldInfo.
      var MyTypea : Type = Type.GetType("Myfielda");
      var Myfieldinfoa : FieldInfo = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance);
      var MyTypeb : Type = Type.GetType("Myfieldb");
      var Myfieldinfob : FieldInfo = MyTypeb.GetField("field", BindingFlags.NonPublic|BindingFlags.Static);
 
      //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);
   }
}
Myfieldinfo.Main();

This code produces the following output:

Reflection.FieldInfo

Myfielda - A private field; IsStatic - False

Myfieldb - B static field; IsStatic - True

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

FieldInfo Class | FieldInfo Members | System.Reflection Namespace | FieldAttributes | GetField | Type

Show: