> To attach a method to an event, we can use EventInfo.AddEventHandler.
>
> After attached, given an event's name, is it possible to retrieve all
> methods attached to it?

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
> No. An event is really just a pair of methods, add and remove. Although
> *usually* there's a delegate variable backing it, the add/remove can do
[quoted text clipped - 3 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
OK I'm just curious...
Yeah I could not find a way to track, but, it should be somewhere in
the memory, right? Existing as a linked list, or inside an internal
Class... Unfortunately I don't know CIL (MSIL), and reflector stops at
the mysterious abstract Method "abstract MethodInfo GetAddMethod":
[DebuggerHidden, DebuggerStepThrough]
public void AddEventHandler(object target, Delegate handler)
{
MethodInfo addMethod = this.GetAddMethod();
if (addMethod == null)
{
throw new
InvalidOperationException(Environment.GetResourceString("InvalidOperation_NoPublicAddMethod"));
}
addMethod.Invoke(target, new object[] { handler });
}
public MethodInfo GetAddMethod()
{
return this.GetAddMethod(false);
}
public abstract MethodInfo GetAddMethod(bool nonPublic);
Jon Skeet [C# MVP] - 23 Nov 2007 20:46 GMT
> > No. An event is really just a pair of methods, add and remove. Although
> > *usually* there's a delegate variable backing it, the add/remove can do
[quoted text clipped - 3 lines]
> Yeah I could not find a way to track, but, it should be somewhere in
> the memory, right?
Usually, but not necessarily. For instance, you *could* write an event
that did nothing with the event handlers that you added:
public event EventHandler Foo
{
add {}
remove {}
}
Or here's an oddity - it adds to one delegate variable or another,
depending on the time of day (and removes from both):
EventHandler foo1;
EventHandler foo2;
public event EventHandler Foo
{
add
{
if (DateTime.Now.Hour >= 12)
{
foo1 += value;
}
else
{
foo2 += value;
}
}
remove
{
foo1 -= value;
foo2 -= value;
}
}
I'm not saying it's *sensible* (and in particular the removal side is
nasty) but it's valid code as far as C# and the CLR are concerned.
> Existing as a linked list, or inside an internal
> Class... Unfortunately I don't know CIL (MSIL), and reflector stops at
> the mysterious abstract Method "abstract MethodInfo GetAddMethod":
It's not mysterious - it finds which method is called when you add an
event handler.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk