Bruce Wood <br*******@canada.com> wrote:
I need to remove all handlers from a particular event. I am doing this
in the class where the event is defined.
I have this vague memory that I can say:
this.MyEvent = null;
and this will clear all delegates from the event, but I'm not sure, and
I can't find the relevant documentation.
Yes, you can - *if* the event has been declared as a "field-like" event
within the same class.
Can I do that, or do I have to loop through each delegate like this:
foreach (Delegate d in this.MyEvent.GetInvocationList())
{
this.MyEvent -= d;
}
Basically, a "field-like" event is actually two things:
1) The event (a pair of add/remove methods)
2) A delegate variable
You can do what you like to a delegate variable, but the only things
you can do with an event are add and remove handlers (and fire the
event, in theory, but C# doesn't implement that part itself).
What you *can't* do is clear an event declared in another class (even a
base class) with your first try - nor can you get the invocation list.
From the point of view of any other class (at compile-time; reflection
is a different story) the event is *just* an event.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too