Removing anonymous event handlers [duplicate]

可紊 提交于 2019-12-04 00:36:16

问题


Possible Duplicate:
C#: How to remove a lambda event handler

Is it possible to remove an event handler which was attached as anonymous function? Let's say I have an event, and I subscribe to it in this way:

TestClass classs = new TestClass ();
classs.myCustomEvent +=  (a,b) => { Console.Write(""); };

Is it possible somehow to remove this eventHandler using -= ??


回答1:


It is possible, but you need to store it in a local variable first:

MyDelegate handler = (a, b) => { Console.Write(""); };
class.myCustomEvent += handler;
class.myCustomEvent -= handler;


来源:https://stackoverflow.com/questions/6460281/removing-anonymous-event-handlers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!