Export (0) Print
Expand All

FieldInfo.IsStatic Property

Gets a value indicating whether the field is static.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public bool IsStatic { get; }
/** @property */
public final boolean get_IsStatic ()

public final function get IsStatic () : boolean

Not applicable.

Property Value

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 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.

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

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 private 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 = typeof(Myfielda);
        FieldInfo Myfieldinfoa = MyTypea.GetField("field", BindingFlags.NonPublic|BindingFlags.Instance);
        Type MyTypeb = typeof(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;
    }
}

import System.*;
import System.Reflection.*;

// Make two fields.
public class MyFielda
{
    private String field = "A private field";

    /** @property 
     */
    public String get_Field()
    {
        return field;
    } //get_Field

    /** @property 
     */
    public void set_Field(String value)
    {
        if (!(field.Equals(value))) {
            field = value;
        }
    } //set_Field
} //MyFielda

public class MyFieldb
{
    private static String field = "B static field";

    /** @property 
     */
    public String get_Field()
    {
        return field;
    } //get_Field

    /** @property 
     */
    public void set_Field(String value)
    {
        if (!(field.Equals(value))) {
            field = value;
        }
    } //set_Field
} //MyFieldb

public class MyFieldInfo
{
    public static void main(String[] args)
    {
        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.get_FullName());
        Console.Write("{0}; ", myFieldInfoa.GetValue(myFielda));
        Console.Write("IsStatic - {0}", 
            System.Convert.ToString(myFieldInfoa.get_IsStatic()));

        // For the second field get and display the name, field, and 
        // IsStatic property value.
        Console.Write("\n{0} - ", myTypeb.get_FullName());
        Console.Write("{0}; ", myFieldInfob.GetValue(myFieldb));
        Console.Write("IsStatic - {0}", 
            System.Convert.ToString(myFieldInfob.get_IsStatic()));
    } //main
} //MyFieldInfo

//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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft