470,647 Members | 1,223 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

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 5149
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stefan Rosenthal | last post: by
5 posts views Thread by Chris | last post: by
19 posts views Thread by zacks | last post: by
6 posts views Thread by mj2736 | last post: by
5 posts views Thread by Klaudiusz Bryja | last post: by
4 posts views Thread by =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.