Freigeben über


FieldInfo.IsInitOnly-Eigenschaft

Ruft einen Wert ab, der angibt, ob das Feld nur im Rumpf des Konstruktors festgelegt werden kann.

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

Syntax

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

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

Eigenschaftenwert

true, wenn für das Feld das InitOnly-Attribut festgelegt ist, andernfalls false.

Hinweise

Wenn als Wert true zurückgegeben wird, kann das Feld lediglich initialisiert werden und ist anschließend schreibgeschützt.

Um die IsInitOnly-Eigenschaft abzurufen, rufen Sie zunächst den Type der Klasse ab. Über Type rufen Sie FieldInfo ab. Über FieldInfo rufen Sie die IsInitOnly-Eigenschaft ab. Kombinieren Sie für den Zugriff auf ein nicht öffentliches Feld BindingFlags.NonPublic in der GetField-Methode entweder mit BindingFlags.Static oder BindingFlags.Instance oder mit beidem.

Die IsInitOnly-Eigenschaft ist festgelegt, wenn das FieldAttributes.InitOnly-Attribut festgelegt ist.

Beispiel

Im folgenden Beispiel werden zwei Felder erstellt. Das zweite Feld ist schreibgeschützt und verfügt über keinen set-Accessor. Außerdem ist IsInitOnly auf true festgelegt.

Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic


'Make two fields, one public and one read-only.
Public Class Myfielda
    Public field As String = "A - public modifiable field"
End Class 'Myfielda

Public Class Myfieldb
    Public ReadOnly field As String = "B - readonly field"
End Class 'Myfieldb

Public Class Myfieldinfo
    Public Shared Function Main() As Integer
        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("field", _
            BindingFlags.Public Or BindingFlags.Instance)
        Dim MyTypeb As Type = GetType(Myfieldb)
        Dim Myfieldinfob As FieldInfo = MyTypeb.GetField("field", _
            BindingFlags.Public Or BindingFlags.Instance)

        'Modify the fields.
        'Note that Myfieldb is not modified, as it is
        'read-only (IsInitOnly is True).
        Myfielda.field = "A - modified"
        'For the first field, get and display the name, field, and IsInitOnly state.
        Console.WriteLine("{0} - {1}, IsInitOnly = {2} ", MyTypea.FullName, _
            Myfieldinfoa.GetValue(Myfielda), Myfieldinfoa.IsInitOnly)
        'For the second field get and display the name, field, and IsInitOnly state.
        Console.WriteLine("{0} - {1}, IsInitOnly = {2} ", MyTypeb.FullName, _
            Myfieldinfob.GetValue(Myfieldb), Myfieldinfob.IsInitOnly)

        Return 0
    End Function 'Main
End Class 'Myfieldinfo
using System;
using System.Reflection;

 //Make two fields, one public and one read-only.
public class Myfielda
{
    public string field = "A - public modifiable field";
}
public class Myfieldb
{
    public readonly string field = "B - readonly field";
}
 
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.Public | BindingFlags.Instance);
        Type MyTypeb = typeof(Myfieldb);
        FieldInfo Myfieldinfob = MyTypeb.GetField("field",
            BindingFlags.Public | BindingFlags.Instance);
  
        //Modify the fields.
        //Note that Myfieldb is not modified, as it is
        //read-only (IsInitOnly is True).
        Myfielda.field = "A - modified";
        //Myfieldb.field = "B - modified";
  
        //For the first field, get and display the name, field, and IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
            MyTypea.FullName,
            Myfieldinfoa.GetValue(Myfielda),
            Myfieldinfoa.IsInitOnly);
  
        //For the second field get and display the name, field, and IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
            MyTypeb.FullName,
            Myfieldinfob.GetValue(Myfieldb),
            Myfieldinfob.IsInitOnly);
  
        return 0;
    }
}
using namespace System;
using namespace System::Reflection;

//Make two fields, one public and one read-only.
public ref class Myfielda
{
public:
   String^ field;
   Myfielda()
      : field( "A - public field" )
   {}


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

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

   }

};

public ref class Myfieldb
{
private:
   String^ const field;

public:
   Myfieldb()
      : field( "B - readonly field" )
   {}


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

   }

};

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::Public | BindingFlags::Instance) );
   Type^ MyTypeb = Type::GetType( "Myfieldb" );
   FieldInfo^ Myfieldinfob = MyTypeb->GetField( "field", static_cast<BindingFlags>(BindingFlags::NonPublic | BindingFlags::Instance) );
   
   //Modify the fields.
   //Note that Myfieldb is not modified, as it is
   //read-only (IsInitOnly is True).
   myfielda->field = "A- modified";
   
   //Myfieldb.field = "B- modified";
   //For the first field, get and display the name, field, and IsInitOnly state.
   Console::Write( "\n{0} - {1}, IsInitOnly = {2} ", MyTypea->FullName, Myfieldinfoa->GetValue( myfielda ), Myfieldinfoa->IsInitOnly );
   
   //For the second field get and display the name, field, and IsInitOnly state.
   Console::Write( "\n{0} - {1}, IsInitOnly = {2} ", MyTypeb->FullName, Myfieldinfob->GetValue( myfieldb ), Myfieldinfob->IsInitOnly );
   return 0;
}
import System.*;
import System.Reflection.*;

//Make two fields, one public and one read-only.
public class MyFielda
{
    public String field = "A - public 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 final String field = "B - readonly field";

    /** @property 
     */
    public String get_Field()
    {
        return field;
    } //get_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.Public | BindingFlags.Instance);
        Type myTypeb = Type.GetType("MyFieldb");
        FieldInfo myFieldInfob = myTypeb.GetField("field", 
            BindingFlags.NonPublic | BindingFlags.Instance);

        //Modify the fields.
        //Note that myfieldb is not modified, as it is
        //read-only (IsInitOnly is True).
        myFielda.field = "A- modified";

        //myfieldb.field = "B- modified"; 
        //For the first field, get and display the name, field, and 
        // IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ", 
            myTypea.get_FullName(), myFieldInfoa.GetValue(myFielda), 
            System.Convert.ToString(myFieldInfoa.get_IsInitOnly()));

        //For the second field get and display the name, field, and 
        //IsInitOnly state.
        Console.Write("\n{0} - {1}, IsInitOnly = {2} ", 
            myTypeb.get_FullName(), myFieldInfob.GetValue(myFieldb), 
            System.Convert.ToString(myFieldInfob.get_IsInitOnly()));
    } //main
} //MyFieldInfo
//Make two fields, one public and one read-only.
public class Myfielda
{
   public var field : String = "A public field";
   public function get Field() : String {
       return field;
   }
   public function set Field( value : String ) {
       if (field!=value) field=value;
   }
}

public class Myfieldb
{
   const field : String = "B readonly field";
   public function get Field() : String {
       return field;
   }
}

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.Instance|BindingFlags.Public);
      var MyTypeb : Type = Type.GetType("Myfieldb");
      var Myfieldinfob : FieldInfo = MyTypeb.GetField("field", BindingFlags.Instance|BindingFlags.Public);
 
      //Modify the fields.
      //Note that Myfieldb is not modified, as it is
      //read-only (IsInitOnly is True).
      myfielda.field = "A- modified";
      //Myfieldb.field = "B- modified";
 
      //For the first field, get and display the name, field, and IsInitOnly.
      Console.Write("\n{0} - {1}, IsInitOnly = {2} ",
         MyTypea.FullName,
         Myfieldinfoa.GetValue(myfielda),
         Myfieldinfoa.IsInitOnly);
 
      //For the second field get and display the name, field, and IsInitOnly.
      Console.Write("\n{0} - {1}, IsLiteral = {2} ",
         MyTypeb.FullName,
         Myfieldinfob.GetValue(myfieldb),
         Myfieldinfob.IsLiteral);
   }
}
Myfieldinfo.Main();

Dieser Code erzeugt die folgende Ausgabe:

Reflection.FieldInfo

Myfielda - A- modified, IsInitOnly = False

Myfieldb - B readonly field, IsInitOnly = 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