Solved

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

Posted on 2009-04-05
2
506 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now