Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

VB.Net: Getting a private field using reflection

From Wiki

Jump to: navigation, search
  1. Imports System.IO
  2.  
  3. Module Module1
  4.  
  5.     Sub Main()
  6.         Dim _test As Test
  7.         _test = New Test
  8.         Console.WriteLine(_test.Id)
  9.         Dim p As System.Reflection.FieldInfo = _test.GetType.GetField("_id", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
  10.         Console.WriteLine(p.GetValue(_test))
  11.         p.SetValue(_test, 1)
  12.         Console.WriteLine(p.GetValue(_test))
  13.         Console.WriteLine(_test.Id)
  14.         Console.ReadLine()
  15.     End Sub
  16.  
  17. End Module
  18.  
  19. Public Class Test
  20.     Private _id As Integer
  21.  
  22.     Public Sub New()
  23.         _id = 0
  24.     End Sub
  25.  
  26.     Public ReadOnly Property Id() As Integer
  27.         Get
  28.             Return _id
  29.         End Get
  30.     End Property
  31. End Class

The magic happens here.

  1. p.GetValue(_test)

this gets the value you specified here

  1. Dim p As System.Reflection.FieldInfo = _test.GetType.GetField("_id", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)

535 Rating: 1.3/5 (3 votes cast)