System Reflection Gestione Eventi

giovedì 29 agosto 2013 - 13.49
Tag Elenco Tags  VB.NET  |  .NET 4.0  |  Windows 7  |  Visual Studio 2010

rbob Profilo | Newbie

Ho un serivizio windows che deve caricare dei plug-in (dll vb net) in funzione di particolari impostazioni.
Quindi utilizzo la System.Reflection per caricare la dll.

La dll contiene delle funzioni e degli eventi pubblici che vengono eseguiti a seconda delle necessità.

Esempio codice della dll (miaDLL.DLL):

Public Class MiaCLASSE
public Event mioEvento(byval sDato as string)


public sub miaSub(byval sS1 as string, byval sS2 as string)
msgbox("Ok "+sS1+" " +sS2)

RaiseEvent mioEvento("Evento inviato")
end sub


End Class



Nel servizio ho creato una funzione che carica un dato Assembly (dll di plug-in) e chiama la funzione : miaSub

Il codice è il seguente:

If System.IO.File.Exists("c:\miaDLL.DLL") Then
Try
Dim asm As Assembly = Assembly.LoadFrom("c:\miaDLL.DLL")
Dim myType As Type = asm.GetType("miaDLL.MiaCLASSE")
Dim myctor As ConstructorInfo = myType.GetConstructor(Type.EmptyTypes)
Dim myMetodo As MethodInfo = myType.GetMethod("miaSub")
Dim myInstance As Object = myctor.Invoke(Nothing)

Dim myParameters() As Object = {"aaa", "bbbb"}
myMetodo.Invoke(myInstance, myParameters) 'La chiamata funziona. Verificato!

Dim myEvent As EventInfo = myType.GetEvent("mioEvento")
If Not (myEvent Is Nothing) Then


PROBLEMA: come faccio ad intercettare l'evento ("mioEvento") nel mio gestore eventi

AddAndler myEvent, AddressOf Manage_MyEvent ' non va bene

End If

Catch ex As Exception
MsgBox("Impossibile caricare: c:\miaDLL.DLL " + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace)

End Try

End If


Private Sub Manage_MyEvent(ByVal aaa As String)
MsgBox(aaa)
End Sub


Qualcuno ha qualche idea?

ciao ho risolto da solo in questo modo

If Not (myEvent Is Nothing) Then
'Create a new instance
Dim instance As Object = Activator.CreateInstance(myType)
Dim d As [Delegate] = [Delegate].CreateDelegate(myEvent.EventHandlerType, Me, "Manage_MyEvent")
myEvent.AddEventHandler(instance, d)

'Test call
Dim methodToCall As MethodInfo = myType.GetMethod("miaSub")
Dim str(0 To 1) As String
str(0) = "test1"
str(1) = "test2"
methodToCall.Invoke(instance, str)
End If

0v3rCl0ck Profilo | Guru

ciao, ho visto che hai già risolto il problema, accetta questa risposta, che chiudiamo il thread. grazie
-------------------------------------------------------
Michael Denny
Lead Software Developer & Solutions Architect
http://blogs.dotnethell.it/Regulator/
http://dennymichael.wordpress.com
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5