Now here is the really interesting part - You can also call a method using MethodInfo.
Let's say that you had the name of a method stored in a variable. You can't go - variablename(). So you have to use MethodInfo.
MethodInfo.Invoke takes two parameters - The instance that created the method and the parameters (in an object array).
Take a look at the following code -
Public Class WebForm3 Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim mtype As Type = GetType(WebForm3) Dim minfo As System.Reflection.MethodInfo = mtype.GetMethod("Return1") Dim myparamarray() As Object = {"Sample Name"} 'New Response.Write(minfo.Invoke(Me, myparamarray)) 'New End Sub
Public Function Return1(ByVal name As String) As String Return "Hello " & name End Function
End Class |
The two new lines create a parameter array with a field for each parameter and it's value and then uses minfo.Invoke to call the function and write it's results to the screen. The same would work with a method.