471,071 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Reflection Question: Determining and invoking a Control's event handlers

Hi

Is it possible, using Reflection, to determine at runtime the method(s) that
are provided as handler for a given controls events?

Also, once discovered, is it possible to manually invoke these methods?

For example, suppose I have a ImageButton on a webform that has an on click
handler associated. I would like to be able to discover the name of the
event handling method(s) that handle this event, and manually invoke them.

Kind regards

Ben

Nov 16 '05 #1
3 1573
Ben,
Is it possible, using Reflection, to determine at runtime the method(s) that
are provided as handler for a given controls events?


Only by reflecting on private members, and doing that on code you
don't control is asking for trouble, since it can easily break in the
future.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi Mattias,

Do you have an example of how to do this this, or know where I can find one?

Kind regards

Ben
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
Ben,
Is it possible, using Reflection, to determine at runtime the method(s) thatare provided as handler for a given controls events?


Only by reflecting on private members, and doing that on code you
don't control is asking for trouble, since it can easily break in the
future.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
Ben,
Do you have an example of how to do this this, or know where I can find one?


using System;
using System.Reflection;

delegate void FooEventHandler();

class Events
{
public event FooEventHandler Foo;
}

class Test
{
static void Handler1() { Console.WriteLine( "Handler1" ); }
static void Handler2() { Console.WriteLine( "Handler2" ); }

static void Main()
{
Events e = new Events();
e.Foo += new FooEventHandler(Handler1);
e.Foo += new FooEventHandler(Handler2);

Delegate d = (Delegate)e.GetType().GetField("Foo",
BindingFlags.NonPublic|BindingFlags.Instance).GetV alue(e);
foreach ( FooEventHandler f in d.GetInvocationList() )
f();
}
}

Now this assumes that the underlying delegate field is named the same
as the event (Foo in this case). That's not always true, and there's
no way to get the field name from the event name. There may not even
be a single underlying delegate field for each event.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

44 posts views Thread by craig | last post: by
7 posts views Thread by Tiraman | last post: by
3 posts views Thread by James Ramaley | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.