我写了一些类:
public class A
{
public A()
{
serviceAdapter.CompletedCallBackEvent += new EventHandler( foo );
.
.
.
}
void foo(object sender, EventArgs e)
{
serviceAdapter.CompletedCallBackEvent -= new EventHandler( foo );
}
}
现在,我想用一些匿名方法更改此回调监听器 - 但我不知道如何删除匿名方法中的回调监听器。
class A
{
public A()
{
serviceAdapter.CompletedCallBackEvent += delegate( object sender, EventArgs ee )
{
... need to remove the listener to the event.
}
}
}
最佳答案
您可以简单地将您的委托(delegate)/处理程序分配给一个私有(private)变量。
private EventHander _handler = null;
public A()
{
_handler = delegate( object sender, EventArgs ee)
{
ServiceAdapter.CompletedCallBackEvent -= _handler;
};
ServiceAdapter.CompletedCallBackEvent += _handler;
}
关于c# - 如何删除匿名方法上的回调监听?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6252603/