13

Lets say we have a delegate

public delegate void MyEventHandler(string x);

and an event handler

public event MyEventHandler Something;

we add multiple events..

for(int x = 0; x <10; x++)  
{
   this.Something += HandleSomething;
}

My question is .. how would one remove all methods from the eventhandler presuming one doesn't know its been added 10 (or more or less) times?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Eminem
  • 7,206
  • 15
  • 53
  • 95

2 Answers2

28

Simply set the event to null:

this.Something = null;

It will unregister all event handlers.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 2
    I always thought one had to manually remove them, but surprisingly.. this does work.. I did something similar the accepted answer on http://stackoverflow.com/questions/447821/how-do-i-unsubscribe-all-handlers-from-an-event-for-a-particular-class-in-c – Eminem Mar 18 '16 at 12:57
  • 1
    It seems to me that this practice is well worth doing inside a class' Dispose(bool) method, but I don't see references to this practice anywhere. Is my thinking sound? – Pieter Geerkens Aug 25 '17 at 12:55
  • 1
    I think that could be a good idea, but you should be careful not to bloat your code either, which could end up leading to bugs instead of fixing them. I have a method where I pass in the register and unregister action and on calling `Dispose` it executes all unregister actions. @PieterGeerkens – Patrick Hofman Aug 25 '17 at 13:04
  • I thought this wouldn't be possible because of this : https://stackoverflow.com/a/153744/62921 Or is it possible in that particular case ? – ForceMagic Sep 13 '17 at 17:24
  • @ForceMagic this is from within the class itself. So it is possible. – Patrick Hofman Sep 13 '17 at 17:37
  • Oh that's great then, I'll try it out! Thanks – ForceMagic Sep 13 '17 at 17:45
0

As pseudo idea:

C#5 <

class MyDelegateHelperClass{
    public static void RemoveEventHandlers<TModel, TItem>(MulticastDelegate m, Expression<Func<TModel, TItem>> expr) {


                EventInfo eventInfo= ((MemberExpression)expr.Body).Member as EventInfo;


                Delegate[] subscribers = m.GetInvocationList();

                Delegate currentDelegate;

                for (int i = 0; i < subscribers.Length; i++) {

                    currentDelegate=subscribers[i];
                    eventInfo.RemoveEventHandler(currentDelegate.Target,currentDelegate);

                }
            }
}

Usage:

 MyDelegateHelperClass.RemoveEventHandlers(MyDelegate,()=>myClass.myDelegate);

C#6

public static void RemoveEventHandlers(this MulticastDelegate m){

        string eventName=nameof(m);

        EventInfo eventInfo=m.GetType().ReflectingType.GetEvent(eventName,BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);


        Delegate[] subscribers = m.GetInvocationList();

        Delegate currentDelegate;

        for (int i = 0; i < subscribers.Length; i++) {

            currentDelegate=subscribers[i];
            eventInfo.RemoveEventHandler(currentDelegate.Target,currentDelegate);

        }

    }

Usage:

MyDelegate.RemoveEventHandlers();
user999913
  • 336
  • 2
  • 8