示例#1
0
        /// <summary>
        /// 当消息分发程序将发送消息完成时发生
        /// </summary>
        /// <param name="e">事件数据</param>
        protected virtual void OnDispatched(MessageDispatchEventArgs e)
        {
            var temp = this.Dispatched;

            if (temp != null)
            {
                temp(this, e);
            }
        }
示例#2
0
        /// <summary>
        /// 派发消息
        /// </summary>
        /// <param name="message">要派发的消息</param>
        public virtual void DispatchMessage <T>(T message)
        {
            var messageType = typeof(T);

            if (this.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);
                    }
                }
            }
        }