Dynamic Load of Assembly - Event-Handler ???

  • Thread starter Thread starter Stefan Rosenthal
  • Start date Start date
S

Stefan Rosenthal

Hi all,

I've an OL-Style App loading MDIChild-Windows (Panes) on demand dynamicaly
at runtime.
Loading the Forms works fine but establishing an EventHandler (to receive
information from
the loaded Panes by the Container) fails with the Message "Cannot bind to
...." in the line where
the Delegate should be created.

The Sub should load the Assembly (WinForm) and establish an EventHandler to
the local Sub
"PlugInRequestBroker" which would handle the requested Action.

Has anyone an idea how the Event-Handle could be established (my first Try:
AddHandler ...
did not work).

Thx for any help!
Stefan Rosenthal

The Event is declared in an Interface which is implemented by the loaded
Assembly/Form.
The Sub "PlugInRequestBroker" is declared in the same class as
"LoadPaneFromAssembly"
(in the MDIParent).

Code Snippet:

Private Function LoadPaneFromAssembly(ByVal pFile As String, ByVal pType As
String) As Object

Dim objPane As Object
Dim objAssembly As System.Reflection.Assembly
Dim objType As Type

Try
objAssembly = System.Reflection.Assembly.LoadFrom(pFile)
objType = objAssembly.GetType(pType)
objPane = System.Activator.CreateInstance(objType)

' Event-Handler für Request-Event installieren
Try
Dim requestEvent As System.Reflection.EventInfo = _
objType.GetEvent("Request")
Dim eventHandlerInfo As System.Reflection.MethodInfo = _
Me.GetType.GetMethod("PlugInRequestBroker")
Dim eventHandlerType As System.Type = _
requestEvent.EventHandlerType
Dim requestDelegate As System.Delegate = _
System.Delegate.CreateDelegate(eventHandlerType,
eventHandlerInfo, "PlugInRequestBroker") <= Failure !!!
requestEvent.AddEventHandler(Me, requestDelegate)
Catch ex As Exception
MsgBox(ex.Message)
End Try

Catch ex As Exception
objPane = Nothing
MsgBox("Error in LoadPaneFromAssembly: " & ex.Message)
End Try

Return objPane

End Function
 
I had this problem and managed to work out what was going on, you need to declare your method as shared as createDelegate only supports creating delegates to static methods. for example, public shared sub PlugInRequestBroker.
 
Back
Top