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