在下面的代码,我想通过导出/继承它来扩展一个类的行为,并利用基类的事件:为什么事件不能在派生类中以与C#中的基类相同的方式使用?
public class A
{
public event EventHandler SomeEvent;
public void someMethod()
{
if(SomeEvent != null) SomeEvent(this, someArgs);
}
}
public class B : A
{
public void someOtherMethod()
{
if(SomeEvent != null) SomeEvent(this, someArgs); // << why is this not possible?
//Error: The event 'SomeEvent' can only appear on the left hand side of += or -=
//(except when used from within the type 'A')
}
}
为什么不是这可能吗?
什么是这种情况的通用解决方案?
不要使用前缀`提高...`,它应该永远是`在...`。基类的方法也应该是虚拟的。 – Keith 2008-10-31 14:55:47
也感谢多线程的提示。 – 2008-10-31 14:55:58