Link to home
Start Free TrialLog in
Avatar of pratham
pratham

asked on

Error binding to Target Method - When hooking up a delegate through reflection

I would like to hookup a delegate through reflection but I'm getting an Error binding to Target method.  Below is the code I'm working on:

Partial Public Class PopupTemplate
    Private Sub AddEvent(ByVal thisUIElement As UIElement)
        Dim obj As Object = thisUIElement
        Dim mfevent As EventInfo = obj.GetType().GetEvent("ItemChanged")
        Dim tDelegate As Type = mfevent.EventHandlerType
        Dim miHandler As MethodInfo = GetType(PopupTemplate).GetMethod("BtnClicked", _
                                       BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance Or BindingFlags.Public)

        Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, miHandler)
        Dim miAddHandler As MethodInfo = mfevent.GetAddMethod()
        Dim addHandlerArgs() As Object = {d}
        miAddHandler.Invoke(obj, addHandlerArgs)
    End Sub

    Private Sub BtnClicked(ByVal sender As Object, ByVal Value As String)

        If Value = "Save" Then
            CloseForm()
        End If
    End Sub

    Private Sub CloseForm()
 'Does Something
    End Sub

End Class

Partial Public Class Layout3
    Public Event ItemChanged(ByVal s As Object, ByVal Value As String)

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        RaiseEvent ItemChanged(sender, "Save")
    End Sub
End Class

I referenced the example in MSDN when creating these code.  

Thanks
Avatar of unmeshdave
unmeshdave
Flag of India image

what is the error you are getting?
ASKER CERTIFIED SOLUTION
Avatar of pratham
pratham

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial