SoFunction
Updated on 2025-03-06

C# Method to remove all event bindings

This article describes how C# removes all event bindings. Share it for your reference. The specific analysis is as follows:

private delegate int DEL_TEST_EventHandler(int m, int n);
private event DEL_TEST_EventHandler DelTestEventHandler;
/// <summary>
/// Remove all event bindings/// </summary>
/// <param name="clearEvent"></param>
private void clear_event(DEL_TEST_EventHandler clearEvent)
{
 Delegate[] dels = ();
 foreach (Delegate d in dels)
 {
  //Get the method name  object delObj = ().GetProperty("Method").GetValue(d, null);
  string funcName = (string)().GetProperty("Name").GetValue(delObj, null);
    (funcName);
  DelTestEventHandler -= d as DEL_TEST_EventHandler;
 }
}
//Test the main functionprivate void test()
{
 DelTestEventHandler += add;
 DelTestEventHandler += sub;
   DelTestEventHandler += add;
 DelTestEventHandler += sub;
   clear_event(DelTestEventHandler);
}
private int add(int m, int n)
{
 return m + n;
}
private static int sub(int m, int n)
{
 return m - n;
}

I hope this article will be helpful to everyone's C# programming.