|
-
May 27th, 2005, 03:55 PM
#1
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:
Dim oClass As Object
Dim tType As Type
'cResult is an assembly that is compiled from the code that I load from a file
oClass = cResult.CompiledAssembly.CreateInstance("clsScripting")
tType = cResult.CompiledAssembly.GetType("clsScripting")
Dim oArguments(1) As Object
oArguments(0) = m_App
'oClass.customcode(m_App)
tType.InvokeMember("CustomCode", Reflection.BindingFlags.Default Or BindingFlags.InvokeMethod, Nothing, oClass, oArguments)
Here is the code it is compiling:
VB Code:
Class clsScripting
Public Sub CustomCode(ByVal objWord As Object)
objWord.Selection.TypeText("A")
End Sub
End Class
Any help would be appreciated.
Thanks,
-
May 27th, 2005, 04:11 PM
#2
Hyperactive Member
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
-
Jun 1st, 2005, 09:26 AM
#3
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?
-
Jun 1st, 2005, 07:57 PM
#4
Re: Using InvokeMember
Shouldn't this be:
VB Code:
Dim oArguments(0) As Object
oArguments(0) = m_App
Also you didn't say what error you got?
-
Jun 2nd, 2005, 05:46 AM
#5
Hyperactive Member
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 !
-
Jun 2nd, 2005, 08:42 AM
#6
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.
-
Jun 2nd, 2005, 10:38 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|