Dynamically Calling Methods
page 6 of 7
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 29867/ 75

Invoking

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.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-01 4:50:09 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search