我是Java开发人员,慕名学习C# 还是初学者,
请教一个C# 匿名委托转换成普通事件的写法。谢谢!
//code-01
WebSocket.OnOpen += (sender, e) =>
{
...
};
//code-02
WebSocket.OnMessage += (sender, e) =>
{
....
};
//因为匿名的没有办法做-=的操作,代码扫描工具会提示事件泄露,
//——我想把上面两个代码+=右边的转化成这种形式,
WebSocket.OnOpen += Variables
WebSocket.OnOpen -= Variables
//请问 变量Variables应该怎么定义 ?
PS.这个WebSocket库是:
PM> Install-Package WebSocketSharp -Pre
WebSocket定义:
public event System.EventHandler<MessageEventArgs> OnMessage;
public event System.EventHandler OnOpen;
是不是这个意思
class Class1 {
public void Test() {
WebSocket webSocket = new WebSocket();
webSocket.OnMessage += WebSocket_OnMessage;
webSocket.OnOpen += WebSocket_OnOpen;
webSocket.OnMessage -= WebSocket_OnMessage;
}
private void WebSocket_OnOpen(object sender, EventArgs e) {
throw new NotImplementedException();
}
private void WebSocket_OnMessage(object sender, MessageEventArgs e) {
throw new NotImplementedException();
}
class WebSocket {
public event System.EventHandler<MessageEventArgs> OnMessage;
public event System.EventHandler OnOpen;
}
}
正常情况,你在vs里面,打完WebSocket.OnOpen以后直接按两次tab键,试试。。。