谁能给出解决方案?
public class BaseOne{ public event EventHandler OperationChanged; } public class Derived: BaseOne { private void OnChange(){ if(OperationChanged!=null){ OperationChanged(this,new EventArgs()); } } } public Class MainClass { public void Main() { Derived d = new Derived(); d.OperationChanged += OnOperationChange; } private void OnOperationChange(object sender, EventArgs e) { MessageBox("Hello I'm Called"); } }
我试过这样的话,你能提出更好的方法吗?提前致谢 :)
你不能直接解雇基类事件.
这是一个有用的模式.
using System; class Base { public event EventHandler operationsChanged; public void OnOperationsChanged(EventArgs e) { operationsChanged(this,e); } } class Derived : Base { public void OnSpecificOperationChanged() { OnOperationsChanged(EventArgs.Empty); } } class Test { static void Main() { Derived d = new Derived(); d.operationsChanged += OnOperationsChanged; //invoke d.OnSpecificOperationChanged(); } static void OnOperationsChanged(object sender, EventArgs e) { Console.WriteLine("called"); } }
https://msdn.microsoft.com/en-us/library/hy3sefw3.aspx