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

Re: Event Reflection by Greg

Greg
Fri Sep 12 16:27:09 CDT 2003

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" <bluekiteNO-SPAM@rogers.com> wrote in message
news:eGq6N8WeDHA.696@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
>
>



Re: Event Reflection by John

John
Fri Sep 12 23:46:14 CDT 2003

"BK" <bluekiteNO-SPAM@rogers.com> wrote in message
news:eGq6N8WeDHA.696@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
john.saunders@surfcontrol.com

}



Re: Event Reflection by Eric

Eric
Sat Sep 13 19:38:33 CDT 2003

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" <bluekiteNO-SPAM@rogers.com> wrote in message
news:eGq6N8WeDHA.696@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
>
>



Re: Event Reflection by BK

BK
Mon Sep 15 08:35:18 CDT 2003

Yes, I tried this, but there is no way to get the value of event delegate.

"Eric Gunnerson [MS]" <ericgu@online.microsoft.com> wrote in message
news:#nKaIileDHA.1836@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" <bluekiteNO-SPAM@rogers.com> wrote in message
> news:eGq6N8WeDHA.696@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
> >
> >
>
>



Re: Event Reflection by BK

BK
Mon Sep 15 08:37:26 CDT 2003

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:#XQX4SXeDHA.3788@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" <bluekiteNO-SPAM@rogers.com> wrote in message
> news:eGq6N8WeDHA.696@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
> >
> >
>
>