Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VB.Net: setting a private field using reflection
From Wiki
We use the same code as for setting a readonly property.
- Imports System.IO
- Module Module1
- Sub Main()
- Dim _test As Test
- _test = New Test
- Console.WriteLine(_test.Id)
- Dim p As System.Reflection.FieldInfo = _test.GetType.GetField("_id", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
- p.SetValue(_test, 1)
- Console.WriteLine(_test.Id)
- Console.ReadLine()
- End Sub
- End Module
- Public Class Test
- Private _id As Integer
- Public Sub New()
- _id = 0
- End Sub
- Public ReadOnly Property Id() As Integer
- Get
- Return _id
- End Get
- End Property
- End Class



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.