1 回答

TA貢獻1898條經驗 獲得超8個贊
您當前的代碼存在幾個問題:
OnEventRaise
當您應該使用事件本身時,您正在訂閱該事件(即)process
。使用 lambda初始化操作的語法
showName
不正確。你需要使用Action showName = () => { // ... };
.
a.process -+ showName;
最后,要取消訂閱該事件,您可以簡單地在操作主體內部使用,但您需要先對其進行初始化以防止編譯器抱怨。
在你的班級嘗試這樣的事情B
:
public class B
{
// subscribe the closure and delete it once it is invoked, can unsubscribe at anytime.
void subscribe(A a)
{
string name = "one shot subscriber";
Action showName = null;
showName = () =>
{
print(name);
a.process -= showName;
};
a.process += showName;
}
private void print(string s)
{
// ....
}
}
- 1 回答
- 0 關注
- 138 瀏覽
添加回答
舉報