Dale said:
Actually, you can enumerate the registered event handlers using
Delegate.GetInvocationList(). This I have done many times.
That is for *delegates*, not *events*. Try calling GetInvocationList()
on the Button class's Click event.
There are also a couple methods of the Delegate class that may be the answer
to the OP's question but I have never used them so I can't say for certain.
The RemoveAll or RemoveImpl may be able to do what he asks.
Assigning null does exactly what the OP needs, he wanted to clear all
delegates assigned to an event. RemoveAll() applied to every delegate
that was added to a delegate *will* result in null:
---8<---
using System;
using System.Reflection;
class App
{
static void Handler(object sender, EventArgs e)
{
}
static void Main(string[] args)
{
EventHandler h = Handler;
h = (EventHandler) Delegate.RemoveAll(h,
new EventHandler(Handler));
Console.WriteLine("h == null? {0}", h == null);
}
}
--->8---
Additionally, keep in mind that when adding a new member to the invocation
list of an event, we use += specifically in order to not erase the previous
registered handlers. Therefore, if you say
Button1.Click = null;
You can't do that from outside the class, as I mentioned in my previous
post.
that should clear all of the event handlers from the button click event.
It won't. Try this:
---8<---
using System.Windows.Forms;
class App
{
static void Main(string[] args)
{
new Button().Click = null;
}
}
--->8---
You'll get a compiler error:
error CS0079: The event 'System.Windows.Forms.Control.Click' can only
appear on the left hand side of += or -=
The reason is that there are only two methods associated with the event
implemnetation, add_* and remove_*, so there is no mechanism for
clearing all the events.
-- Barry