Event Reflection

B

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
 
G

Greg Ewing [MVP]

BK, I don't understand what you are trying to do. What do you want that
GetInvocationList() doesn't give you?
 
J

John Saunders

BK said:
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
(e-mail address removed)

}
 
E

Eric Gunnerson [MS]

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.
 
B

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top