473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1662
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
2
by: Wiktor Zychla | last post by:
Let's start with an example: if you have: class Test { public delegate int MyDelegate( int n ); event MyDelegate MyEvent; } then you can get MyEvent using reflection:
7
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
3
by: James Ramaley | last post by:
I have created a VB.NET User Control which inherits from TextBox. I have a general question regarding how functionality should be overwritten: I can either write a method which handles an event:...
2
by: Grzegorz Danowski | last post by:
Hello, I'd like to hide control if any of its event handlers is forbidden for current user (basing on PrincipalPermissionAttribute). How to list all methods that handle given event? I supposed...
4
by: Spectre1337 | last post by:
Hello, to my understanding the ASP.NET process works as follows: 1) Page_Init(): At this moment the user should allocate his controls, set their IDs and connect them to the page...
8
by: jehugaleahsa | last post by:
Hello: I am creating a simple class library for simplifying reflection. I am creating an EventWrapper class that allows programmers to treat it like an event (minus the operator overloading)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.