VB.net get variable values and reflected by calling the function through reflection

Imports System.Reflection

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Debug.Print(getvalue("xy"))
        'Debug.Print(getvalue("xyz"))
        'Debug.Print(getvalue("x"))

        Dim mt As MethodInfo = GetType(Form1).GetMethod("add")
        MsgBox(mt.Invoke(Me, {11, 22}))
    End Sub


    Function add(aa, bb)
        Return aa + bb
    End Function

    Public x = 100
    Public y = 200
    Public z = 300

    Public Function getvalue(s As String)
        Dim ret = ""
        For Each i In s.ToArray
            ret = ret + i & "," & GetType(Form1).GetField(i).GetValue(Me) & vbCrLf
        Next
        Return ret
    End Function

End Class

 

Published 10 original articles · won praise 0 · Views 4657

Guess you like

Origin blog.csdn.net/aa326358942/article/details/104325744