How to remove all event handlers from an event

后端 未结 18 1851
再見小時候
再見小時候 2020-11-22 01:20

To create a new event handler on a control you can do this

c.Click += new EventHandler(mainFormButton_Click);

or this

c.Cli         


        
18条回答
  •  日久生厌
    2020-11-22 01:46

    This is not an answer to the OP, but I thought I'd post this here in case it can help others.

      /// 
      /// Method to remove a (single) SocketAsyncEventArgs.Completed event handler. This is 
      /// partially based on information found here: http://stackoverflow.com/a/91853/253938
      /// 
      /// But note that this may not be a good idea, being very .Net implementation-dependent. Note 
      /// in particular use of "m_Completed" instead of "Completed".
      /// 
      private static void RemoveCompletedEventHandler(SocketAsyncEventArgs eventArgs)
      {
         FieldInfo fieldInfo = typeof(SocketAsyncEventArgs).GetField("m_Completed", 
                                                    BindingFlags.Instance | BindingFlags.NonPublic);
         eventArgs.Completed -= (EventHandler)fieldInfo.GetValue(eventArgs);
      }
    

提交回复
热议问题