增强的事件组件

前 注:这是自己平时根据自己需要写的一些小代码,为了避免再次数据丢失,所以放到网上来,未必对各看官有用。另外,这是根据个人想法而写,未必严谨和符合设计原则,若有任何不妥之处,还请不吝赐教。

特 性:

1、可在事件所有者外部触发。

2、增加Enabled属性用于禁用事件。使得无需清空响应和重新添加响应,便可达到暂时屏蔽事件的效果。

3、可异步响应。.NET内置事件的响应在调用者线程上执行。

4、事件委托限定为EventHandler<T>。

说 明:

1、一般情况下,在所有者外触发事件代表着不好的设计。事件既属于某个类,则触发它的权限自然也应属于且仅属于此类。但并非所有的情况都如此,例如有些事件既可通过界面操作触发,也可后台触发,甚至间接触发。

2、此组件在内部封装一个.NET事件,为了初始化该事件,此组件本应为泛型,其参数为事件的委托。但出于以下原因或考虑:一、.NET的泛型无法限定为委托类型(Delegate);二、对外提供强类型控制;三、EventHandler<T>为微软推荐的事件响应委托。故将事件委托限定为EventHandler<T>,此组件的泛型参数对应EventHandler<T>中的T。

示 例:略。

源 码:

    /// <summary>
    /// 事件源类,此类相当于扩展的事件
    /// </summary>
    /// <typeparam name="T">事件参数类型</typeparam>
    public class EventSource<T> where T:EventArgs
    {
        #region Fields

        /// <summary>
        /// 事件对象
        /// </summary>
        private event EventHandler<T> _Event;

        /// <summary>
        /// 是否可用
        /// </summary>
        private bool _Enabled = true;

        /// <summary>
        /// 异步触发事件的线程池调用接口委托
        /// </summary>
        private WaitCallback _AsyncRaiseCallBack;

        #endregion

        #region Properties

        /// <summary>
        /// 事件对象
        /// </summary>
        public event EventHandler<T> Event
        {
            add
            {
                this._Event += value;
            }
            remove
            {
                this._Event -= value;
            }
        }

        /// <summary>
        /// 是否可用:若设置为true,则Raise函数将不会触发事件
        /// </summary>
        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

        /// <summary>
        /// 异步触发事件的线程池调用接口委托
        /// </summary>
        private WaitCallback AsyncRaiseCallBack
        {
            get
            {
                if (_AsyncRaiseCallBack == null)
                {
                    _AsyncRaiseCallBack = new WaitCallback(RaiseEventCallBack);
                }
                return _AsyncRaiseCallBack;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="sender">发送方</param>
        /// <param name="args">参数</param>
        public void Raise(object sender, T args)
        {
            if (Enabled && _Event != null)
            {
                _Event(sender, args);
            }
        }

        /// <summary>
        /// 异步触发事件
        /// </summary>
        /// <param name="sender">发送方</param>
        /// <param name="args">参数</param>
        /// <param name="Async">是否异步触发</param>
        public void Raise(object sender, T args,bool Async)
        {
            if (!Enabled || _Event == null) return;

            if (Async)
            {
                Pair<object, T> context = new Pair<object, T>(sender, args);
                ThreadPool.QueueUserWorkItem(AsyncRaiseCallBack, context);
            }
            else
            {
                _Event(sender, args);
            }
        }

        /// <summary>
        /// 异步触发事件的线程池入口函数
        /// </summary>
        private void RaiseEventCallBack(object ThreadContext)
        {
            Pair<object, T> context = ThreadContext as Pair<object, T>;
            _Event(context.First, context.Second);
        }

        /// <summary>
        /// 添加响应
        /// </summary>
        /// <param name="Response">响应</param>
        /// <returns>事件源</returns>
        public static EventSource<T> operator +(EventSource<T> Source, EventHandler<T> Response)
        {
            Source.Event += Response;
            return Source;
        }

        /// <summary>
        /// 添加响应
        /// </summary>
        /// <param name="Response">响应</param>
        /// <returns>事件源</returns>
        public static EventSource<T> operator -(EventSource<T> Source, EventHandler<T> Response)
        {
            Source.Event -= Response;
            return Source;
        }

        /// <summary>
        /// 添加响应
        /// </summary>
        /// <param name="Response"></param>
        public void Add(EventHandler<T> Response)
        {
            this._Event += Response;
        }

        public void Remove(EventHandler<T> Response)
        {
            this._Event -= Response;
        }

        #endregion
    }
原文地址:https://www.cnblogs.com/yedaoq/p/1691236.html