static void Main11(string[] args) { ListenerOne listener = new ListenerOne(); SenderBase sb = new SenderBase(); SenderChild sc = new SenderChild(); listener.Register(sb); listener.Register(sc); string s = "It comes!"; int i = 100; sb.SomethingHappens(i, s); sc.SomethingHappens(i, s); listener.Unregister(sb); listener.Unregister(sc); ManyEventsClass mec = new ManyEventsClass(); mec.e1 += TempHandleMany_1; //背后的实现是自定义的, 而不是默认自动生成的event+= mec.e2 += TempHandleMany_2; mec.e3 += TempHandleMany_3; mec.Happen1(); mec.Happen2(i, s); //mec.Happen3(); }
public void Unregister(SenderBase sb) { //不用担心删除一个没添加过的方法, 最终调用Delegate.Remove方法在这种情况只是什么也不做 sb.myEvent -= EventHandlerOne; }
//Listener提供让自己注册和取消监听事件的方法 public void Register(SenderBase sb) { sb.myEvent += EventHandlerOne; }