Add an event handler to a loaded dll from outside project
Posted on 2009-04-19
I have an application that loads plugins which are external dll's within my exe directory using
Dim A As Assembly = Assembly.LoadFrom(fFile.FullName)
Dim Plugin As Object = A.CreateInstance(fFile.Name.ToString.Replace(fFile.Extension, "") & ".SuperClass")
where fFile.Fullname is the path to my DLL.
Within this dll I know it has a public event called RaiseMessage, and within my application I want to be able to handle this event when the dll raises the event. the event in the DLL is declared such
Public Event RaiseMessage(ByVal hEventMessages As Hashtable)
What I have tried are the following to try and add a handler to this event, but it hasnt worked, so any ideas would be great:
Using the method of the object (of which my project is unaware):
AddHandler Plugin.RaiseError, AddressOf RaiseMessage
RaiseError is not an event of Object
Dim tModule As Type = Plugin.GetType()
Dim mi As MethodInfo = tModule.GetMethod("RaiseMessage", BindingFlags.Public Or BindingFlags.Instance)
AddHandler Plugin.GetType.GetMethod("RaiseMessage"), AddressOf RaiseMessage
'AddHandler' or 'RemoveHandler' statement event operand must be a dot-qualified expression or a simple name.