MethodInfo.Invoke on unknown method of ActiveX control at runtime

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



OK so weird scenario but it's what the client wants so here it goes...

I have a bunch of ActiveX/OCX controls which I have run through AxImp to
generate wrappers.
These controls all have a somewhat generic interface and each include a
subset of Event Handlers including:

LoadApp
LaunchApplication
ChangeTitle
Resize
Unload

Resize and Unload are generic EventHandlers and I have written code that
works fine for these but the others are custom event handlers. As an
example, for the control MyControl the LaunchApplication event handlers would
have the following in the wrapper class Types:


void add_LaunchApplication(__MyControl_LaunchApplicationEventHandler)
void remove_LaunchApplication(__MyControl_LaunchApplicationEventHandler)


additionally, the ctor for __MyControl_LaunchApplicationEventHandler is
defined as:


__MyControl_LaunchApplicationEventHandler(object object, IntPtr method)
__GridDisplay_LaunchApplicationEventHandler(object object, IntPtr method)
<-- other example
__LabelControl_LaunchApplicationEventHandler(object object, IntPtr method)
<-- other example


I got the above information from Reflection API on MyControl, the above
methods and event handler at runtime.

So at runtime I have a instance of the MyControl wrapper control and
interrogate it to get the MethodInfo instance for add_LaunchApplication then
interogate further to get the ParameterInfo for the method parameter as
follows:


01 For Each meth As System.Reflection.MethodInfo In
(ctrl.GetType().GetMethods())
02 Select Case (meth.Name.ToLower())
03 Case "add_launchapplication"
04
05 Dim args() As Object
06 ReDim args(0)
07
08 Dim ctorArgs() As Object
09 ReDim ctorArgs(1)
10 ctorArgs(0) = ctrl.TopLevelControl
11 ctorArgs(1) = ???what here???
12
13 args(0) =
Activator.CreateInstance(meth.GetParameters()(0).ParameterType, _
14 Reflection.BindingFlags.Public, _
15 Nothing, _
16 ctorArgs, _
17 Nothing)
18 meth.Invoke(ctrl, _
19 Reflection.BindingFlags.InvokeMethod, _
20 Nothing, _
21 args, _
22 Nothing)
23
24 End Select
25 Next


I can call the method directly, that's fine, but I still need to instantiate
an instance of the event handler which I will not know until runtime. The
lines I'm having problems with are 08-17. I am assuming that the first ctor
arg (object) is the instance object mosting the event handler method and the
second (method) is a pointer to the event handler method. But how do I get
this value at runtime? Anyone have any suggestions? Am I overlooking an
obvious alternative? Thanks.



Mike
.



Relevant Pages

  • Re: Event Handler doesnt fire immediately upon postback?
    ... make its comparisons and work with the PostBack event handlers. ... It seems like such a waste of resources to me sometimes to use controls like ... I still prefer to do as much client-side processing as possible ... the event handler fires immediately. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Help me with threads.
    ... > controls from a worker thread, I disagree that dealing with this situation ... > public delegate void StringEventHandler(object worker, StringEventArgs e); ... > part, BeginInvoke needs a pointer to our event handler, plus an object ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Help me with threads.
    ... What you need to do is to define an event that your worker thread can throw ... when it needs to send data back to the UI thread, create an event handler to ... sure that you're in the correct thread before you update your UI controls. ... StringEventArgs e); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Event Handler doesnt fire immediately upon postback?
    ... ASP.Net programming model, coming from Classic ASP, which is entirely ... Controls within Controls in the Page, which is, itself, a Control. ... previous state (prior to the PostBack), and only then does the Page (and ... the event handler fires immediately. ...
    (microsoft.public.dotnet.framework.aspnet)