FieldInfo.IsInitOnly Property tells whether the field can only be set in the body of the constructor.
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic
Public Class Myfielda
Public field As String = "public modifiable field"
End Class 'Myfielda
Public Class Myfieldb
Public ReadOnly field As String = "readonly field"
End Class 'Myfieldb
Public Class Myfieldinfo
Public Shared Function Main() As Integer
Dim Myfielda As New Myfielda()
Dim Myfieldb As New Myfieldb()
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)
Myfielda.field = "A - modified"
Console.WriteLine(MyTypea.FullName)
Console.WriteLine(Myfieldinfoa.GetValue(Myfielda))
Console.WriteLine(Myfieldinfoa.IsInitOnly)
Console.WriteLine(MyTypeb.FullName)
Console.WriteLine(Myfieldinfob.GetValue(Myfieldb))
Console.WriteLine(Myfieldinfob.IsInitOnly)
Return 0
End Function 'Main
End Class 'Myfieldinfo
Related examples in the same category