Clear All Event subscriptions (Clone linked)

Posted by mattias on Stack Overflow See other posts from Stack Overflow or by mattias
Published on 2010-05-24T14:27:43Z Indexed on 2010/05/24 14:31 UTC
Read the original article Hit count: 148

Filed under:
|
|

I just implemented Clone from ICloneable and realized that the event subscriptions from my source instance also followed. Is there a good way to clear all those?

Currently I am using a couple of these loops for every event I have to clear everything.

foreach (var eventhandler in OnIdChanged.GetInvocationList())
{
    OnIdChanged -= (ItemEventHandler) eventhandler;
}

foreach (var eventhandler in OnNameChanged.GetInvocationList())
{
    ...

This works fine but clutters the code a bit. Mostly worried to get event dangling.

© Stack Overflow or respective owner

Related posts about c#

Related posts about events