Freigeben über


FieldInfo.IsInitOnly Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public ReadOnly Property IsInitOnly As Boolean
public bool IsInitOnly { get; }

Property Value

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

Exceptions

Exception Condition
MethodAccessException

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

Remarks

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.

Examples

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.

Imports System.Reflection

Public Class Example

   'Make two public fields, one read-only.
   Public Myfielda As String = "A - public modifiable field"
   Public ReadOnly Myfieldb As String = "B - readonly field"

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) 

      outputBlock.Text &= "Reflection.FieldInfo" & vbCrLf & vbCrLf

      Dim ex As New Example()

      'Get the Type and FieldInfo.
      Dim myType As Type = GetType(Example)
      Dim fa As FieldInfo = myType.GetField("Myfielda", _
          BindingFlags.Public Or BindingFlags.Instance)
      Dim fb As FieldInfo = myType.GetField("Myfieldb", _
          BindingFlags.Public Or 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} ", _
                                        fa.Name, _
                                        fa.GetValue(ex), _
                                        fa.IsInitOnly) & vbCrLf
      'For the second field get and display the name, field, and IsInitOnly state.
      outputBlock.Text &= String.Format("{0} - ""{1}"", IsInitOnly = {2} ", _
                                        fb.Name, _
                                        fb.GetValue(ex), _
                                        fb.IsInitOnly) & vbCrLf
   End Sub

End Class 

'This code produces the following output: 
'
'Reflection.FieldInfo 
'Myfielda - "A - modified", IsInitOnly = False 
'Myfieldb - "B - readonly field", IsInitOnly = 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", 
                                        fa.Name, 
                                        fa.GetValue(ex), 
                                        fa.IsInitOnly);
      //For the second field get and display the name, field, and IsInitOnly state.
      outputBlock.Text += String.Format("{0} - \"{1}\", IsInitOnly = {2} \n", 
                                        fb.Name, 
                                        fb.GetValue(ex), 
                                        fb.IsInitOnly);
   }
}

/*This code produces the following output: 

Reflection.FieldInfo 
Myfielda - "A - modified", IsInitOnly = False 
Myfieldb - "B - readonly field", IsInitOnly = True 
 */

Version Information

Silverlight

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

Platforms

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