473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1673
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.o rg> wrote in message
news:uG******** ******@tk2msftn gp13.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.Reflecti on;

delegate void FooEventHandler ();

class Events
{
public event FooEventHandler Foo;
}

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

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

Delegate d = (Delegate)e.Get Type().GetField ("Foo",
BindingFlags.No nPublic|Binding Flags.Instance) .GetValue(e);
foreach ( FooEventHandler f in d.GetInvocation List() )
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
7364
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
44
4213
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
2
6168
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
1386
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
1405
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: function MyControl_Enter(...) handles MyControl.Enter or I can overwrite the relevant virtual method:
2
2749
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 that I should use reflection and I examined EventInfo class but I haven't found in this class any information about handlers list, but there is a possibility to add one (AddEventHandler method). --
4
14190
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 (Controls.Add()). 2) ASP.NET is parsing the ViewState and the rest of the HTTP Request. It now synchronizes the controls that the user has allocated in Page_Init() according to their ID. 3) Page_Load(): At this point the user controls should be initialized...
8
1708
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). I am concerned with how to check that handlers have been added to the event prior to raising it.
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.