This topic has not yet been rated - Rate this topic

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)
'Declaration
Public ReadOnly Property IsInitOnly As Boolean

Property Value

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

Implements

_FieldInfo.IsInitOnly

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.

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

This code produces the following output:

Reflection.FieldInfo

Myfielda - A- modified, IsInitOnly = False

Myfieldb - B readonly field, IsInitOnly = True

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.