Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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