/**************************************************************** ** 文件名: ActionDelegatedEventHandler.cs ** 主要类: ActionDelegatedEventHandler类 ** Copyright (c) 章为忠 ** 创建人: ** 日 期: 2017.3.10 ** 修改人: ** 日 期: ** 修改内容: ** 描 述: ** 版 本: ** 备 注: ****************************************************************/ using System; namespace Weiz.EventBus.Core { /// /// Represents the event handler which delegates the event handling process to /// a given delegated method. /// /// The type of the event to be handled by current handler. public sealed class ActionDelegatedEventHandler : IEventHandler where TEvent : IEvent { #region Private Fields private readonly Action action; #endregion #region Ctor /// /// Initializes a new instance of ActionDelegatedEventHandler{TEvent} class. /// /// The instance that delegates the event handling process. public ActionDelegatedEventHandler(Action action) { this.action = action; } #endregion #region Public Methods /// /// Returns a value which indicates whether the current /// ActionDelegatedEventHandler{T} equals to the given object. /// /// The which is used to compare to /// the current ActionDelegatedEventHandler{T} instance. /// If the given object equals to the current ActionDelegatedEventHandler{T} /// instance, returns true, otherwise, false. public override bool Equals(object obj) { if (ReferenceEquals(this, obj)) return true; if (obj == null) return false; ActionDelegatedEventHandler other = obj as ActionDelegatedEventHandler; if (other == null) return false; return Delegate.Equals(this.action, other.action); } #endregion #region IHandler Members /// /// Handles the specified message. /// /// The message to be handled. public void Handle(TEvent message) { action(message); } #endregion } }