Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-05
2
Medium Priority
?
514 Views
Last Modified: 2013-11-12
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
0
Comment
Question by:pratham
2 Comments
 
LVL 8

Expert Comment

by:unmeshdave
ID: 24082846
what is the error you are getting?
0
 

Accepted Solution

by:
pratham earned 0 total points
ID: 24086762
Hello,

Thanks for the response unmeshdave.  I was getting an "Error binding to Target Method" error.  

I fixed my problem though.

In the addEvent, instead of writing this line of code:
Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, miHandler)
It should have been
Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, Me, miHandler)

Thanks,

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question