Freigeben über


FieldInfo.IsStatic-Eigenschaft

Ruft einen Wert ab, der angibt, ob das Feld statisch ist.

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

Syntax

'Declaration
Public ReadOnly Property IsStatic As Boolean
'Usage
Dim instance As FieldInfo
Dim value As Boolean

value = instance.IsStatic
public bool IsStatic { get; }
public:
virtual property bool IsStatic {
    bool get () sealed;
}
/** @property */
public final boolean get_IsStatic ()
public final function get IsStatic () : boolean

Eigenschaftenwert

true, wenn das Feld statisch ist, andernfalls false.

Hinweise

Bei einem statischen Feld greifen alle Instanzen des Typs auf eine Kopie des Felds zu.

Die IsStatic-Eigenschaft ist festgelegt, wenn das FieldAttributes.Static-Attribut festgelegt ist.

Um die IsStatic-Eigenschaft abzurufen, rufen Sie zunächst den Type der Klasse ab. Über Type rufen Sie FieldInfo ab. Über FieldInfo rufen Sie die IsStatic-Eigenschaft ab. Zum Zugreifen auf ein nicht öffentliches Feld wird in der GetField-Methode BindingFlags auf NonPublic festgelegt, und der Zugriff wird auf Instance oder Static festgelegt.

Beispiel

Im folgenden Beispiel wird bestimmt, ob das angegebene Feld statisch ist, und das Ergebnis wird angezeigt.

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 private 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 = GetType(Myfielda)
        Dim Myfieldinfoa As FieldInfo = _
           MyTypea.GetField("m_field", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim MyTypeb As 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
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;
    }
}
using namespace System;
using namespace System::Reflection;

// Make two fields.
public ref class Myfielda
{
private:
   String^ field;

public:
   Myfielda()
      : field( "A private field" )
   {}


   property String^ Field 
   {
      String^ get()
      {
         return field;
      }

      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }

   }

};

public ref class Myfieldb
{
private:
   static String^ field = "B static field";

public:

   property String^ Field 
   {
      String^ get()
      {
         return field;
      }

      void set( String^ value )
      {
         if ( field != value )
         {
            field = value;
         }
      }

   }

};

int main()
{
   Console::WriteLine( "\nReflection.FieldInfo" );
   Myfielda^ myfielda = gcnew Myfielda;
   Myfieldb^ myfieldb = gcnew Myfieldb;
   
   // Get the Type and FieldInfo.
   Type^ MyTypea = Type::GetType( "Myfielda" );
   FieldInfo^ Myfieldinfoa = MyTypea->GetField( "field", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   Type^ MyTypeb = Type::GetType( "Myfieldb" );
   FieldInfo^ Myfieldinfob = MyTypeb->GetField( "field", static_cast<BindingFlags>(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();

Dieser Code erzeugt die folgende Ausgabe:

Reflection.FieldInfo

Myfielda - A private field; IsStatic - False

Myfieldb - B static field; IsStatic - True

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

FieldInfo-Klasse
FieldInfo-Member
System.Reflection-Namespace
FieldAttributes-Enumeration
GetField
Type