Results 1 to 7 of 7

Thread: Using InvokeMember

  1. #1

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Using InvokeMember

    I am trying to use InvokeMember to call a function from some code I am compiling on the fly. If I turn Option Strict off and just call the method, it works no problem, so I am guessing there is just something wrong with my InvokeMember call:

    VB Code:
    1. Dim oClass As Object
    2.             Dim tType As Type
    3. 'cResult is an assembly that is compiled from the code that I load from a file
    4.             oClass = cResult.CompiledAssembly.CreateInstance("clsScripting")
    5.             tType = cResult.CompiledAssembly.GetType("clsScripting")
    6.  
    7.             Dim oArguments(1) As Object
    8.             oArguments(0) = m_App
    9.  
    10.             'oClass.customcode(m_App)
    11.             tType.InvokeMember("CustomCode", Reflection.BindingFlags.Default Or BindingFlags.InvokeMethod, Nothing, oClass, oArguments)

    Here is the code it is compiling:
    VB Code:
    1. Class clsScripting
    2.     Public Sub CustomCode(ByVal objWord As Object)
    3.         objWord.Selection.TypeText("A")
    4.     End Sub
    5. End Class

    Any help would be appreciated.

    Thanks,

  2. #2
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Using InvokeMember

    I usuall call InvokeMember with

    x.GetType.InvokeMember(CustomMethod, Reflection.Bindings.InvokeMethod, x.GetType.DefaultBidner, x, New Object() {})

    Give it a whirl

    Good Luck

  3. #3

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Using InvokeMember

    Thanks for the advice Duck, but its not working. I can get it to call methods that have no arguments, however, when I try to use arguments, I get the error.

    Anyone know what I am doing wrong?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Using InvokeMember

    Shouldn't this be:
    VB Code:
    1. Dim oArguments(0) As Object
    2.             oArguments(0) = m_App
    Also you didn't say what error you got?

  5. #5
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Using InvokeMember

    Arguments can be passed at the end.

    x.GetType.InvokeMember(CustomMethod, Reflection.Bindings.InvokeMethod, x.GetType.DefaultBidner, x, New Object() {YourObject1, YourObject2, YourObjectN})

    Simply replace your objectN with the object you want to pass as a parameter to your method !

  6. #6

    Thread Starter
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Using InvokeMember

    Thanks Edneeis, that fixed it. Sometimes I forget that creating an array of size 1 actually creates an array of size 2.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: Using InvokeMember

    What happened then was that it tried to find a method with that name and 2 arguments but couldn't find one (since the one you want only has 1 argument). That is how it handles Overloaded methods too, by matching the method name and argument count/types.

    Glad it worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width