Read private fields using Reflection

Imports System.Reflection
Public Class Form1
    Private myNumber As Integer = 25
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
End Class

Module Module1
    Public Function readPvtInt() As Integer
        Dim frmType As Type = GetType(Form1)
        Dim field As FieldInfo = frmType.GetField("myNumber", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim frm As New Form1
        Dim pvtValue As Integer = CInt(field.GetValue(frm))
        Return pvtValue
    End Function
End Module

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s