Solved

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

Posted on 2009-04-05
2
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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