如何才有可能删除“按钮”的“点击”事件的所有事件处理程序?事件、才有、按钮、程序

2023-09-02 10:23:58 作者:丿Asmile、

我有一个按钮控制,而且我需要删除所有连接到它的单击事件。

怎么会是这样可能吗?

 按钮按钮= GetButton();
button.Click.RemoveAllEventHandlers();
 

解决方案

您不能,基本上 - 至少不是没有思考和大量grubbiness的

活动都严格订阅,退订。 - 你不能退订别人的处理程序,任何比你更可以改变别人的对象引用

c 中在一个button事件,点击后要让另一个textbox显示文字内容,

I have a button control, and I'd need to remove all the event handlers attached to its Click event.

How would that be possible?

Button button = GetButton();
button.Click.RemoveAllEventHandlers();

解决方案

You can't, basically - at least not without reflection and a lot of grubbiness.

Events are strictly "subscribe, unsubscribe" - you can't unsubscribe someone else's handler, any more than you can change someone else's reference to an object.