Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FieldInfo.IsInitOnly Property

Gets a value indicating whether the field can only be set in the body of the constructor.

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

public bool IsInitOnly { get; }
/** @property */
public final boolean get_IsInitOnly ()

public final function get IsInitOnly () : boolean

Not applicable.

Property Value

true if the field has the InitOnly attribute set; otherwise, false.

If the returned value is true, the field can only be initialized, and is read-only thereafter.

To get the IsInitOnly property, first get the class Type. From the Type, get the FieldInfo. From the FieldInfo, get the IsInitOnly property. To access a non-public field, combine BindingFlags.NonPublic with either or both of BindingFlags.Static and BindingFlags.Instance in the GetField method.

The IsInitOnly property is set when the FieldAttributes.InitOnly attribute is set.

In the following example, two fields are created. The second field is read-only, having no set accessor, and IsInitOnly is set to true.


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;
    }
}

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();

This code produces the following output:

Reflection.FieldInfo

Myfielda - A- modified, IsInitOnly = False

Myfieldb - B readonly field, IsInitOnly = 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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft