"Jacek Jurkowski" <pc*****@priv1.onet.pl> wrote:
... Clear all delegates assigned to an Event?
From inside the class that declares the event in the default way, one
can assign null to the event:
---8<---
class Test
{
public static event EventHandler Foo;
public static void Bar()
{
Foo = null;
}
}
--->8---
F.e. lets consider the button.
Button.Click += new EventHandler(...);
Button.Click += new EventHandler(...);
From outside the class, you've only got access to the += and -=
operators, which map (internally) to add_<eventName> and
remove_<eventName>. Because you can use this syntax:
---8<---
public event EventHandler Foo
{
add { /* ... */ }
remove { /* ... */ }
}
--->8---
.... there is no way for you to enumerate or otherwise get access to the
implementation details of the storage behind an event handler. The event
handlers could be stored in a hash table or other structure.
So, if you need to clear all events, you must either:
1) Keep track of what you added so you can remove it again later.
or
2) Add a method to the class (if you have written it) which permits
clearing events.
-- Barry
--
http://barrkel.blogspot.com/