C#反编译出来的问题,希望大牛们帮帮忙~~~~
public delegate void OnlineCountChangeEx(int nOnlineCount);
public event CTcpServer.OnlineCountChangeEx OnlineCountChange
{
[MethodImpl(MethodImplOptions.Synchronized)]
add
{
this.OnlineCountChange = (CTcpServer.OnlineCountChangeEx)Delegate.Combine(this.OnlineCountChange, value);
}
[MethodImpl(MethodImplOptions.Synchronized)]
remove
{
this.OnlineCountChange = (CTcpServer.OnlineCountChangeEx)Delegate.Remove(this.OnlineCountChange, value);
}
}
错误:
事件“GprsPlugs.CTcpServer.OnlineCountChange”只能出现在 += 或 -= 的左边
说说什么原因,要如何解决
感激不尽~
------解决方案--------------------GprsPlugs.CTcpServer.OnlineCountChange这应该是事件,c#的事件绑定不能用 = 号,要用 +=,比如
button1.Click += new ...
GprsPlugs.CTcpServer.OnlineCountChange += ...
------解决方案--------------------调用事件需要
OnlineCountChange +=delgate....
不是方法调用
------解决方案--------------------public event CTcpServer.OnlineCountChangeEx OnlineCountChange
{
add { this.OnlineCountChange += value; } remove { this.OnlineCountChange -= value; }
}
------解决方案--------------------
public delegate void OnlineCountChangeEx(int nOnlineCount);
public event CTcpServer.OnlineCountChangeEx OnlineCountChange;
这样
------解决方案--------------------
------解决方案-------------------- public delegate void MyEventHandler();
private MyEventHandler TestAddedEvent;
public event MyEventHandler TestAdded
{
add
{
TestAddedEvent= (MyEventHandler)System.Delegate.Combine(TestAddedEvent, value);
}
remove
{
TestAddedEvent= (MyEventHandler)System.Delegate.Remove(TestAddedEvent, value);
}
}
------解决方案--------------------class1
{
public delegate void OnlineCountChangeEx(int nOnlineCount);
public event CTcpServer.OnlineCountChangeEx OnlineCountChange;
public void test()
{
//只能在当前类中这样使用
if(OnlineCountChange != null)
{
OnlineCountChange(5);
}
}
}
class2
{
public void test()
{
class1 c1 = new class1();
//这里报错,不能是event声明的类以外这样使用。
if(c1.OnlineCountChange != null)
{
}
}
}
上面的add 和 remove 方法是编译器制动实现的"+="(add)注册事件,"-="(remove)移除事件
------解决方案--------------------
------解决方案--------------------按钮事件的 也就是 一个委托的方法!