locked
System Reflection RRS feed

  • Question

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

    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
                       


            End If

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

          End Try

        End If



    Qualcuno ha qualche idea?
    Thursday, August 29, 2013 9:34 AM

Answers

  • Hi BOB,

    According to your description, i think we can use EventInfo.AddEventHandler method to add event handler to an event source.

    Public Overridable Sub AddEventHandler ( target As Object, handler As Delegate )

    handler
    Type: System.Delegate
    Encapsulates a method or methods to be invoked when the event is raised by the target.

    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

    Please check the following references:

    #EventInfo.AddEventHandler Method
    http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx

    #Delegate.CreateDelegate Method (Type, Object, String)
    http://msdn.microsoft.com/en-us/library/12f294ye.aspx


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by 12345_BOB Friday, August 30, 2013 8:38 AM
    Friday, August 30, 2013 4:21 AM
  • HI!
    Franklin

    Your indication is ok and has solved my problem.
    thanks for your time
    • Marked as answer by 12345_BOB Friday, August 30, 2013 8:39 AM
    Friday, August 30, 2013 8:39 AM

All replies

  • Why can't you post in English?
    Thursday, August 29, 2013 9:38 AM
  • I have a windows Services that it has to load the plug-in (dll vb net) depending on the particular settings.
    Then use the System.Reflection.

    The dll contains functions and public events that are performed as needed.

    Example code of the 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("Event dispatched"")
        end sub    


    End Class


    In the service I created a function that loads a given assembly (dll plug-in) and calls the function: miaSub

    The code is as follows:

         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")
             MyInstance As Object Dim myctor.Invoke = (Nothing)

             Dim myParameters () As Object = {"aaa", "bbbb"}
             myMetodo.Invoke (myInstance, myParameters)            'The call works. Verified!

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

             
               PROBLEM: How do I trap the event ("mioEvento") in my event handler
               
               AddAndler myEvent, AddressOf Manage_MyEvent  ' (!?!?!?!?)
                       


             end If

           Catch ex As Exception
             MsgBox ("Unable to load: 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

    Does anyone have any idea?
    Thursday, August 29, 2013 11:07 AM
  • Hi BOB,

    According to your description, i think we can use EventInfo.AddEventHandler method to add event handler to an event source.

    Public Overridable Sub AddEventHandler ( target As Object, handler As Delegate )

    handler
    Type: System.Delegate
    Encapsulates a method or methods to be invoked when the event is raised by the target.

    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

    Please check the following references:

    #EventInfo.AddEventHandler Method
    http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx

    #Delegate.CreateDelegate Method (Type, Object, String)
    http://msdn.microsoft.com/en-us/library/12f294ye.aspx


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by 12345_BOB Friday, August 30, 2013 8:38 AM
    Friday, August 30, 2013 4:21 AM
  • HI!
    Franklin

    Your indication is ok and has solved my problem.
    thanks for your time
    • Marked as answer by 12345_BOB Friday, August 30, 2013 8:39 AM
    Friday, August 30, 2013 8:39 AM