FieldInfo.IsInitOnly Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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)

abstract IsInitOnly : bool with get
override IsInitOnly : bool with get

Property Value

Type: System.Boolean

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.

No code example is currently available or this language may not be supported.

This code produces the following output:

Reflection.FieldInfo

Myfielda - A- modified, IsInitOnly = False

Myfieldb - B readonly field, IsInitOnly = True

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: