/// <summary> /// 消息分发失败处理函数 /// </summary> /// <param name="e"></param> protected virtual void OnDispatchFailed(MessageDispatchEventArgs e) { var temp = this.DispatchFailed; if (temp != null) { temp(this, e); } }
/// <summary> /// 分发消息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="message"></param> public virtual void DispatchMessage <T>(T message) { Type messageType = typeof(T); if (_handlers.ContainsKey(messageType)) { var messageHandlers = _handlers[messageType]; foreach (var messageHandler in messageHandlers) { var dynMessageHandler = (IHandler <T>)messageHandler; var evtArgs = new MessageDispatchEventArgs(message, messageHandler.GetType(), messageHandler); this.OnDispatching(evtArgs); try { dynMessageHandler.Handle(message); this.OnDispatched(evtArgs); } catch { this.OnDispatchFailed(evtArgs); } } } }