2
我以前可以利用以下:事件默认初始
public event EventHandler OnComplete = delegate { };
我不知道,这是怎么叫,这是一个“事件默认初始值”?
但问题似乎是从EventArgs派生出来的,创建了我自己的EventHandler并决定使用相同的方法。请参阅:
public class MyEventArgs : EventArgs
{
int result;
public int Result
{
get
{
if (exceptionObject == null)
return result;
else
throw new InvalidOperationException();
}
internal set { result = value; }
}
Exception exceptionObject;
public Exception ExceptionObject
{
get { return exceptionObject; }
internal set { exceptionObject = value; }
}
}
public delegate EventHandler MyEventHandler(object sender, MyEventArgs e);
public class MyOperation
{
public event MyEventHandler OnOperationComplete = delegate { };
}
所以,行
public event MyEventHandler OnOperationComplete = delegate { };
导致该问题。
如何为“我的”事件进行正确的默认初始化?
返回值的类型肯定是'void',这是...我是今天失明。 '公共事件MyEventHandler OnOperationComplete =委托{};'工作正常。谢谢! – horgh 2012-07-27 03:10:08