我寫了一些類:如何去除匿名方法的回調監聽?
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.
}
}
}
你怎麼可以指定委託私有變量?我試着和它不工作(不編譯) – Yanshof 2011-06-06 13:32:43
@Yanshof:你可以在你的EventHandler類中創建一個私有變量,而不是我上面提到的CompletedCallBackEventHandler(它是一種僞代碼)......你能不能提一下哪種類型的錯誤你越來越?我假設這是「CompletedCallBackEventHandler」上的錯誤你也在你的方法/構造函數中分配這個私有變量 – Hasanain 2011-06-06 13:37:31
我是這樣寫的 - 它不是編譯 – Yanshof 2011-06-06 13:41:09