zoukankan      html  css  js  c++  java
  • EventHandlerList z

    写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.NET提供的EventHandlerList可以辅助多个事件的管 理,但不方便的地方是,它不是类型安全的,缺少类型安全,多少用起来担心会出错。经过改造,可以将系统提供的EventHandlerList通 过泛型提供类型安全的管理。泛型类EventHandlerList.cs的实现如下:

    public sealed class EventHandlerList<T> : IDisposable 
        { 
            ListEntry head;
    
            public EventHandlerList() 
            { }
    
            public Delegate this[T key]
            {
                get{ 
                    ListEntry e = Find(key);
                    return e == null ? null : e.m_handler;
                }
                set{
                    ListEntry e = Find(key);
                    if (e != null){
                        e.m_handler = value;
                    } 
                    else {
                        head = new ListEntry(key, value, head);
                    }
                }
            }
    
            public void AddHandler(T key, Delegate value) 
            {
                ListEntry e = Find(key); 
                if (e != null) {
                    e.m_handler = Delegate.Combine(e.m_handler, value);
                }
                else { 
                    head = new ListEntry(key, value, head);
                } 
            } 
    
            public void AddHandlers(EventHandlerList<T> listToAddFrom) 
            {
                ListEntry currentListEntry = listToAddFrom.head;
                while (currentListEntry != null) { 
                    AddHandler(currentListEntry.m_key, currentListEntry.m_handler);
                    currentListEntry = currentListEntry.m_next; 
                } 
            }
     
            public void Dispose() 
            { 
                head = null;
            } 
     
            private ListEntry Find(T key) 
            {
                ListEntry found = head; 
                while (found != null) {
                    if (found.m_key.Equals(key)) {
                        break;
                    } 
                    found = found.m_next;
                } 
                return found; 
            }
     
            public void RemoveHandler(T key, Delegate value) 
            {
                ListEntry e = Find(key); 
                if (e != null) {
                    e.m_handler = Delegate.Remove(e.m_handler, value);
                } 
            }
     
            private sealed class ListEntry 
            {
                internal ListEntry m_next; 
                internal T m_key; 
                internal Delegate m_handler;
     
                public ListEntry(T key, Delegate handler, ListEntry next) 
                {
                    m_next = next;
                    m_key = key;
                    m_handler = handler; 
                }
            } 
        }

    我们就可以改变多个事件的使用方式,例子类似于下面这个。

    public class DispatcherCore
        {
            readonly EventHandlerList<EventType> Events = new EventHandlerList<EventType>();
    
            public event EventHandler OnReceiveData {
                add {
                    Events.AddHandler(EventType.ReceiveData, value);
                }
                remove {
                    Events.RemoveHandler(EventType.ReceiveData, value);
                }
            }
    
            public event EventHandler OnRefreshData
            {
                add{
                    Events.AddHandler(EventType.RefreshData, value);
                }
                remove{
                    Events.RemoveHandler(EventType.RefreshData, value);
                }
            }
    
            private void RaiseEvent(EventType eventType, EventArgs args)
            { 
                var raiseEvent = Events[eventType] as EventHandler;
    
                if (raiseEvent != null)
                    raiseEvent(this, args);
            }
    
            // 其它逻辑,在适当的时候调用RaiseEvent
    
            private enum EventType
            {
                None,
                ReceiveData,
                RefreshData,
                Error,
                Info,
            }
        }
  • 相关阅读:
    QTP知识总结(一)
    QTP中DataTable操作大全
    QTP DataTable全攻略(1)
    QTP脚本不能录制怎么办?
    每天一个linux命令(20):find命令之exec
    bash下几个替换运算符的区分
    linux_shell 特殊符号的介绍
    Linux dirname、basename 指令
    Liunx readlink命令
    微信公众账号开发教程(四)自定义菜单(转)
  • 原文地址:https://www.cnblogs.com/zeroone/p/3632204.html
Copyright © 2011-2022 走看看