PC Review


Reply
Thread Tools Rate Thread

Error binding to target method

 
 
=?Utf-8?B?VGltIFBhY2w=?=
Guest
Posts: n/a
 
      10th Sep 2007
I appreciate any help with this.

Scenario:
A winforms application that creates a form from WPF Xaml. Catch is that we
cannot use Framework 3.0, must use 2.0. This is not a problem. I just parse
the Xaml as Xml and construct the winforms controls with
Activator.CreateInstance. The problem comes in the events. Our client wants
to allow their clients to specify custom eventhandlers and to provide them in
a custom assembly.

My Code:
temp - the control that fires the event
einfo - the EventInfo for the event
val - the name of the method to be used as eventhandler for the event

private void ControlSetEventHandler(Control temp, EventInfo einfo, string val)
{
Assembly assem = Assembly.Load("ThirdParty.Finance.Assembly");
Type tExForm = assem.GetType("Client.Xforms.XamlGenerator.EventHandlers");
// Get the type of delegate that handles the event.
Type tDelegate = einfo.EventHandlerType;
// If you already have a method with the correct signature,
// you can simply get a MethodInfo for it.
MethodInfo miHandler = tExForm.GetMethod(val, BindingFlags.Public |
BindingFlags.Instance);
// Create an instance of the delegate. Using the overloads
// of CreateDelegate that take MethodInfo is recommended.
Delegate d = Delegate.CreateDelegate(tDelegate, temp, miHandler);
// Get the "add" accessor of the event and invoke it late-
// bound, passing in the delegate instance. This is equivalent
// to using the += operator in C#, or AddHandler in Visual
// Basic. The instance on which the "add" accessor is invoked
// is the form; the arguments must be passed as an array.
MethodInfo addHandler = einfo.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(temp, addHandlerArgs);
}

The Error:
"Error binding to target method"
occurs at line: Delegate d = Delegate.CreateDelegate(tDelegate, temp,
miHandler);

The 3rd party assembly is not referenced by the form project and is located
in the bin/debug folder (I have tried it with the 3rd party assembly added as
a reference). The target method has a signature: public void
MethodName(object sender, EventArgs e). einfo is a Click event. temp is a
button.

This problem only occurs when I get the method from the 3rd party assembly.
If I move the method into the form where ControlSetEventHandler is located
and adjust it to get the event from the Assembly.GetExecutingAssembly it
works fine. That, of course does not meet the clients needs.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error binding to target method =?Utf-8?B?VGltIFBhY2w=?= Microsoft Dot NET Framework 0 10th Sep 2007 05:08 PM
"gridview Object does not match target type" error during binding collection of different type object gui.besse@gmail.com Microsoft ASP .NET 0 9th Mar 2006 11:28 AM
Error binding to target method Oenone Microsoft ASP .NET 0 7th Jun 2005 10:45 AM
"Save Target as" and "Print Target" does not work Tania Windows XP Internet Explorer 2 15th Oct 2003 07:42 AM
Invoke method / Late binding / passing ref variables to a method?? PJ Microsoft C# .NET 0 25th Jul 2003 10:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:53 AM.