How to remove all event handlers from an event

后端 未结 18 1944
再見小時候
再見小時候 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:36

    If you reaallly have to do this... it'll take reflection and quite some time to do this. Event handlers are managed in an event-to-delegate-map inside a control. You would need to

    • Reflect and obtain this map in the control instance.
    • Iterate for each event, get the delegate
      • each delegate in turn could be a chained series of event handlers. So call obControl.RemoveHandler(event, handler)

    In short, a lot of work. It is possible in theory... I never tried something like this.

    See if you can have better control/discipline over the subscribe-unsubscribe phase for the control.

提交回复
热议问题