FieldInfo.IsStatic プロパティ

定義

フィールドが静的かどうかを示す値を取得します。

public:
 property bool IsStatic { bool get(); };
public bool IsStatic { get; }
member this.IsStatic : bool
Public ReadOnly Property IsStatic As Boolean

プロパティ値

フィールドが静的な場合は true。それ以外の場合は false

実装

次の例では、指定したフィールドが静的かどうかを判断し、結果を表示します。

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;
}
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;
    }
}
Imports System.Reflection

' 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

このコードを実行すると、次の出力が生成されます。

Reflection.FieldInfo

Myfielda - A private field; IsStatic - False

Myfieldb - B static field; IsStatic - True

注釈

フィールドが静的な場合、フィールドの 1 つのコピーが型のすべてのインスタンスによって共有されます。

IsStatic属性が設定されると、 FieldAttributes.Static プロパティが設定されます。

プロパティを IsStatic 取得するには、まず クラス Typeを取得します。 から、 Typeを取得します FieldInfo。 から、 FieldInfoプロパティを取得します IsStatic 。 パブリック以外のフィールドにアクセスするには、 メソッドで GetFieldBindingFlagsNonPublic設定し、アクセシビリティを または StaticInstance設定します。

適用対象

こちらもご覧ください