FieldInfo.IsInitOnly Property

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

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

public bool IsInitOnly { get; }

Property Value

Type: System.Boolean
true if the field has the InitOnly attribute set; otherwise, false.


This member is invoked late-bound through mechanisms such as Type.InvokeMember.

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.

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

In the following example, two fields are created. The second field is read-only (that is, it has no set accessor), and therefore the value of IsInitOnly is true.

using System;
using System.Reflection;

public class Example
   //Make two public fields, one read-only.
   public string Myfielda = "A - public modifiable field";
   public readonly string Myfieldb = "B - readonly field";

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      outputBlock.Text += "Reflection.FieldInfo\n\n";

      Example ex = new Example();

      //Get the Type and FieldInfo.
      Type myType = typeof(Example);
      FieldInfo fa = myType.GetField("Myfielda", BindingFlags.Public | BindingFlags.Instance);
      FieldInfo fb = myType.GetField("Myfieldb", BindingFlags.Public | BindingFlags.Instance);

      //Modify the field that is not read-only (that is, the field for which
      //IsInitOnly is False).
      ex.Myfielda = "A - modified";

      //For the first field, get and display the name, field, and IsInitOnly state.
      outputBlock.Text += String.Format("{0} - \"{1}\", IsInitOnly = {2} \n", 
      //For the second field get and display the name, field, and IsInitOnly state.
      outputBlock.Text += String.Format("{0} - \"{1}\", IsInitOnly = {2} \n", 

/*This code produces the following output: 

Myfielda - "A - modified", IsInitOnly = False 
Myfieldb - "B - readonly field", IsInitOnly = True 


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions