Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MethodInfo.Invoke Return Values

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
I am having a problem returning a value from an invoked method. The class I'm instantiating is as follows:

Code:
Public Class DoItClass

    Public Function GetAValue(ByVal p_intType As Integer, ByRef p_strWhichType As String) As String

        Select Case p_intType
            Case 1
                p_strWhichType = "One"
            Case 2
                p_strWhichType = "Two"
        End Select

        Return "GetAValue was accessed"

    End Function
End Class

The app that calls this function looks like the following:
Code:
dim strMethodName As String = "GetAValue"
dim strClassName As string = "DoItClass"
Dim objAssembly As Assembly
Dim objClass As Object
Dim objType As Type
Dim objMethod As MethodInfo
Dim objParamArray() As Object = {1,""}
Dim strReturnMessage As String

try
objAssembly = Assembly.LoadFrom("C:\MyDynamicClass.dll")

For Each objType In objAssembly.GetTypes
    If objType.FullName.EndsWith("." + strClassName) Then
          objClass = objAssembly.CreateInstance(objType.FullName.ToString)
          objMethod = objType.GetMethod(strMethod)
          strReturnMessage = CType(objMethod.Invoke(objClass, objParamArray), String)
          strByRefParm = objParamArray(1).ToString
          exit for
    End If
Next
Catch ex as Exception
     Messagebox.show(ex.message)
End Try

The error I get is "System.Reflection.TargetParameterCountException". When I change my method to a sub and just call the Invoke function without attempting to return anything it executes fine. I feel there's something obvious eluding me here. Thanks for any suggestions.
 
Find the subtle differences.

Code:
[Blue]Imports[/Blue] System.Reflection

[Blue]Public[/Blue] [Blue]Class[/Blue] Form2

    [Blue]Private[/Blue] [Blue]Sub[/Blue] Button1_Click([Blue]ByVal[/Blue] sender [Blue]As[/Blue] System.Object, [Blue]ByVal[/Blue] e [Blue]As[/Blue] System.EventArgs) [Blue]Handles[/Blue] Button1.Click
        [Blue]Dim[/Blue] strMethodName [Blue]As[/Blue] [Blue]String[/Blue] = [Red]"GetAValue"[/Red]
        [Blue]Dim[/Blue] strClassName [Blue]As[/Blue] [Blue]String[/Blue] = [Red]"DoItClass"[/Red]
        [Blue]Dim[/Blue] objAssembly [Blue]As[/Blue] Assembly
        [Blue]Dim[/Blue] objClass [Blue]As[/Blue] Object
        [Blue]Dim[/Blue] objType [Blue]As[/Blue] Type
        [Blue]Dim[/Blue] objMethod [Blue]As[/Blue] MethodInfo
        [Blue]Dim[/Blue] objParamArray() [Blue]As[/Blue] Object = {1, [Red]""[/Red]}
        [Blue]Dim[/Blue] objReturnMessage [Blue]As[/Blue] Object
        [Blue]Dim[/Blue] strbyrefparm [Blue]As[/Blue] [Blue]String[/Blue]

        [Blue]Try[/Blue]
            objAssembly = Assembly.GetExecutingAssembly

            [Blue]For[/Blue] Each objType In objAssembly.GetTypes
                [Blue]If[/Blue] objType.FullName.EndsWith([Red]"."[/Red] + strClassName) [Blue]Then[/Blue]
                    objClass = objAssembly.CreateInstance(objType.FullName.ToString)
                    objMethod = objType.GetMethod(strMethodName)
                    objReturnMessage = objMethod.Invoke(objClass, objParamArray)
                    strByRefParm = objParamArray(1).ToString
                    [Blue]Exit[/Blue] [Blue]For[/Blue]
                [Blue]End[/Blue] [Blue]If[/Blue]
            [Blue]Next[/Blue]
        [Blue]Catch[/Blue] ex [Blue]As[/Blue] Exception
            MessageBox.Show(ex.Message)
        [Blue]End[/Blue] [Blue]Try[/Blue]

    [Blue]End[/Blue] [Blue]Sub[/Blue]
[Blue]End[/Blue] [Blue]Class[/Blue]

[Blue]Public[/Blue] [Blue]Class[/Blue] DoItClass

    [Blue]Public[/Blue] [Blue]Function[/Blue] GetAValue([Blue]ByVal[/Blue] p_intType [Blue]As[/Blue] [Blue]Integer[/Blue], [Blue]ByRef[/Blue] p_strWhichType [Blue]As[/Blue] [Blue]String[/Blue]) [Blue]As[/Blue] [Blue]String[/Blue]

        Select Case p_intType
            Case 1
                p_strWhichType = [Red]"One"[/Red]
            Case 2
                p_strWhichType = [Red]"Two"[/Red]
        [Blue]End[/Blue] Select

        [Blue]Return[/Blue] [Red]"GetAValue was accessed"[/Red]

    [Blue]End[/Blue] [Blue]Function[/Blue]
[Blue]End[/Blue] [Blue]Class[/Blue]


Christiaan Baes
Belgium

"My old site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top