473,395 Members | 1,554 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,395 software developers and data experts.

Event Reflection

BK
Hi,

I have a class which has a lot of events (>100). For some reasons, I have to
go through all invocation lists to do something. What I'm wondering is that,
is there any way to use reflection to get their InvocationList without going
through each event?

// tedious
foreach (Delegate handler in Event1.GetInvocationList()) {...}
foreach (Delegate handler in Event2.GetInvocationList()) {...}
foreach (Delegate handler in Event3.GetInvocationList()) {...}
....
foreach (Delegate handler in Event189.GetInvocationList()) {...}

// user reflection???
.....

Looks like this is a hard question as I haven't googled any answers. Thanks

BK
Jul 21 '05 #1
5 5287
BK, I don't understand what you are trying to do. What do you want that
GetInvocationList() doesn't give you?

--
Greg Ewing [MVP]
http://www.citidc.com

"BK" <bl*************@rogers.com> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class which has a lot of events (>100). For some reasons, I have to go through all invocation lists to do something. What I'm wondering is that, is there any way to use reflection to get their InvocationList without going through each event?

// tedious
foreach (Delegate handler in Event1.GetInvocationList()) {...}
foreach (Delegate handler in Event2.GetInvocationList()) {...}
foreach (Delegate handler in Event3.GetInvocationList()) {...}
...
foreach (Delegate handler in Event189.GetInvocationList()) {...}

// user reflection???
....

Looks like this is a hard question as I haven't googled any answers. Thanks
BK

Jul 21 '05 #2
"BK" <bl*************@rogers.com> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class which has a lot of events (>100). For some reasons, I have to go through all invocation lists to do something. What I'm wondering is that, is there any way to use reflection to get their InvocationList without going through each event?


Are you aware of the alternate way to declare events?

public event EventHandler Something
{
add {add "value" to some list}
remove {remove "value" from the list}
}

The System.Web.UI.Control class defines its events this way. It also
declares an "Events" property of type EventHandlerList. The EventHandlerList
class defines AddHandler and RemoveHandler methods, and an indexer. These
all take an arbitrary object as a parameter:

private static readonly object SomeEvent = new object();

public event EventHandler Something
{
add
{
Events.AddHandler(SomeEvent, value);
}
remove
{
Events.RemoveHandler(SomeEvent, value);
}
}

protected virtual void OnSomething(EventArgs e)
{
EventHandler someEventDelegate = (EventHandler) Events[SomeEvent];
if (someEventDelegate != null)
someEventDelegate(this, e);
}

Now, it seems to me that you could do something similar, perhaps using a
HashTable instead of an EventHandlerList. You would then be able to iterate
over the hashtable and access each event handler in the loop.
--
John Saunders
Internet Engineer
jo***********@surfcontrol.com

}
Jul 21 '05 #3
Sure:

Type type = typeof (YourClassName); // can use instance.GetType()
as well.

foreach (MemberInfo memberInfo in type.GetMembers())
{
if (memberInfo.MemberType == MemberType.Event)

// and then you use MemberType to get the value here. It will probably
come back as as a delegate (I'm not sure).
}

Hope that helps.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"BK" <bl*************@rogers.com> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class which has a lot of events (>100). For some reasons, I have to go through all invocation lists to do something. What I'm wondering is that, is there any way to use reflection to get their InvocationList without going through each event?

// tedious
foreach (Delegate handler in Event1.GetInvocationList()) {...}
foreach (Delegate handler in Event2.GetInvocationList()) {...}
foreach (Delegate handler in Event3.GetInvocationList()) {...}
...
foreach (Delegate handler in Event189.GetInvocationList()) {...}

// user reflection???
....

Looks like this is a hard question as I haven't googled any answers. Thanks
BK

Jul 21 '05 #4
BK
Yes, I tried this, but there is no way to get the value of event delegate.

"Eric Gunnerson [MS]" <er****@online.microsoft.com> wrote in message
news:#n**************@TK2MSFTNGP09.phx.gbl...
Sure:

Type type = typeof (YourClassName); // can use instance.GetType() as well.

foreach (MemberInfo memberInfo in type.GetMembers())
{
if (memberInfo.MemberType == MemberType.Event)

// and then you use MemberType to get the value here. It will probably
come back as as a delegate (I'm not sure).
}

Hope that helps.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "BK" <bl*************@rogers.com> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class which has a lot of events (>100). For some reasons, I
have to
go through all invocation lists to do something. What I'm wondering is

that,
is there any way to use reflection to get their InvocationList without

going
through each event?

// tedious
foreach (Delegate handler in Event1.GetInvocationList()) {...}
foreach (Delegate handler in Event2.GetInvocationList()) {...}
foreach (Delegate handler in Event3.GetInvocationList()) {...}
...
foreach (Delegate handler in Event189.GetInvocationList()) {...}

// user reflection???
....

Looks like this is a hard question as I haven't googled any answers.

Thanks

BK


Jul 21 '05 #5
BK
Basically, I want to get the value of each event delegate using its name
string, like "event1", instead of going through each event to do something
the same, which is very tedious.

Thanks,

"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:#X**************@tk2msftngp13.phx.gbl...
BK, I don't understand what you are trying to do. What do you want that
GetInvocationList() doesn't give you?

--
Greg Ewing [MVP]
http://www.citidc.com

"BK" <bl*************@rogers.com> wrote in message
news:eG*************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a class which has a lot of events (>100). For some reasons, I
have to
go through all invocation lists to do something. What I'm wondering is

that,
is there any way to use reflection to get their InvocationList without

going
through each event?

// tedious
foreach (Delegate handler in Event1.GetInvocationList()) {...}
foreach (Delegate handler in Event2.GetInvocationList()) {...}
foreach (Delegate handler in Event3.GetInvocationList()) {...}
...
foreach (Delegate handler in Event189.GetInvocationList()) {...}

// user reflection???
....

Looks like this is a hard question as I haven't googled any answers.

Thanks

BK


Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
27
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
0
by: Stefan Rosenthal | last post by:
Hi all, I've an OL-Style App loading MDIChild-Windows (Panes) on demand dynamicaly at runtime. Loading the Forms works fine but establishing an EventHandler (to receive information from the...
2
by: Brian Tkatch | last post by:
Not sure if this is the way it's supposed to be, but after a Layout event on an MDI form, the form's new width is set, but the MDIClient's width is not. As an example, start a new Windows...
5
by: Chris | last post by:
Is there any way you can dynamically add an event and then some code. I get it that you could, in your code, add a control and dynamically add an event handler for e.g. the click event. However the...
19
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But...
6
by: mj2736 | last post by:
I created a windows form control that inherits from the standard .Net DataGridView control, to which I've added custom functionality. Instead of using the standard control in my applications, I use...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take...
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: 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
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.