Hi Antinsh,
Youcan pass string of method name to dynamically call method. We needto usethe Type.InvokeMember method. Here is a little example of what you are trying to do. Hope this helps.
Code Snippet
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim d1 As Dispatch = New Dispatch("A")
Dim d2 As Dispatch = New Dispatch("B")
Dim d3 As Dispatch = New Dispatch("C")
d1.Execute()
d2.Execute()
d3.Execute()
Sub
Class Target
Public Sub A()
System.Windows.Forms.MessageBox.Show("This is method A.")
Sub
Public Sub B()
System.Windows.Forms.MessageBox.Show("This is method B.")
End Sub
Class
Dispatch
Private _targetMethod As String
Public Property TargetMethod() As String
Get
Return _targetMethod
End Get
Set(ByVal value As String)
_targetMethod = value
End Set
End Property
Public Sub New(ByVal targetMethod As String)
Me._targetMethod = targetMethod
End Sub
Public Sub Execute()
Dim t As Type = GetType(Target)
Try
t.InvokeMember(TargetMethod, BindingFlags.Instance Or BindingFlags.InvokeMethod Or BindingFlags.Public, Nothing, New Target(), Nothing)
Catch ex As MissingMethodException
System.Windows.Forms.MessageBox.Show("Could not find the supplied method." + Environment.NewLine + ex.Message)
End Try
End Sub
Class
Best regards,
|