Click here to Skip to main content
15,923,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I create a simple web service with four methods namely add, sub, mul, div for performing arithmetic calculations.(VB.Net)
Each method have two arguments. Now I call/invoke a one of the method dynamically. Using reflection.emit namespace, it invoke a method without parameters, below my code
VB
Imports System.Reflection
Imports System.Reflection.Emit

Private Sub InvokeMethod(ByVal instance As Object, ByVal methodName As String)
'Getting the method information using the method info class
Dim mi As MethodInfo = instance.[GetType]().GetMethod(methodName)
'invoking the method
'null- no parameter for the function [or] we can pass the array of parameters
mi.Invoke(instance, Nothing)
  End Sub
 
Private Sub btnCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

Dim obj As New myWebservice.localhost.Service
Dim method As String
method = "HelloWorld" 'the default method
InvokeMethod(obj, method)
MsgBox("Success")
End Sub 
It will Run Greatly..
Private Sub InvokeMethod(ByVal instance As Object, ByVal methodName As String)
        
'Getting the method information using the method info class
Dim mi As MethodInfo = instance.[GetType]().GetMethod(methodName)
Dim a() As Object
a(0) = 12
a(1) = 13
'invoking the method
‘null- no parameter for the function [or] we can pass the array of parameters
mi.Invoke(instance, a(1))
End Sub
Private Sub btnCall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click

Dim obj As New myWebservice.localhost.Service
Dim method As String
method = "add" 'user defined method it have 2 parameters
InvokeMethod(obj, method)
MsgBox("Success")
End Sub 

It will not run I don’t know how to pass args how to catch return value ….
plz give suggestions, Any answers/suggestions higly appreciated
Thanks Regards,
Posted

1 solution

see the example below
I have class A with 2 function HowAreYou and DisableValue.

VB
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As New A
        Dim ret As Object = obj.GetType.InvokeMember("HowAreYou", Reflection.BindingFlags.InvokeMethod, _
                                                     Nothing, obj, Nothing, Nothing)
    End Sub

    Private Sub ExecWithArgs()
        Dim obj As New A
        Dim ret As Object = obj.GetType.InvokeMember("DisableValue", Reflection.BindingFlags.InvokeMethod, _
                                                      Nothing, obj, New Object() {True}, Nothing)
    End Sub

End Class


Public Class A

    Public Function HowAreYou() As String
        Return "Im Fine."
    End Function

    Public Function DisableValue(ByVal val As Boolean) As String
        Return val
    End Function

End Class




the function ExecWithArgs is using arguments and have return value.
maybe this can help you
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900