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: Getting a private field using reflection
From Wiki
- 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)
- Console.WriteLine(p.GetValue(_test))
- p.SetValue(_test, 1)
- Console.WriteLine(p.GetValue(_test))
- 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
The magic happens here.
- p.GetValue(_test)
this gets the value you specified here
- Dim p As System.Reflection.FieldInfo = _test.GetType.GetField("_id", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)



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