Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

CreateDelegate in VS.NET 2003 - error on binding method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
snibril - 23 Nov 2006 13:03 GMT
Hi,

I'm trying to delegate all events of a command button to a single
method, handlesAll.  I wrote the following code in VS.NET 2005, and it
worked fine:

       public void setUpDelegates()
       {

           MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
BindingFlags.Public | BindingFlags.Instance);

           foreach (System.Reflection.EventInfo h in
typeof(Button).GetEvents())

           {
                   h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi));

           }
       }

       public void handlesAll(object sender, EventArgs e)

       {
                  Console.WriteLine("Received event: " +
e.ToString());
       }

As I said, this works fine in VS.NET 2005.  However, in VS.NET 2003,
the overloaded version of CreateDelegate(Type, object, MethodInfo)
doesn't exist - I need to use CreateDelegate(Type, object, string), so
I tried doing

             h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi.Name));

inside the loop.  However, it now gives me the error message:

"An unhandled exception of type 'System.NullReferenceException'
occurred in TestGridEx.exe
Additional information: Object reference not set to an instance of an
object."

I've tried directly calling mi.Invoke() and it finds the method fine -
what am I doing wrong?
snibril - 23 Nov 2006 13:16 GMT
Sorry, posted wrong error message, here is the message I get:

"An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method."

Can anyone help?

> Hi,
>
[quoted text clipped - 42 lines]
> I've tried directly calling mi.Invoke() and it finds the method fine -
> what am I doing wrong?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.