EventHandlerList

本文介绍了一个用于管理事件处理程序的类EventHandlerList。该类通过内部维护的链表管理各种类型的事件处理器,支持添加、移除及查找事件处理器等功能。文章通过具体示例展示了如何使用此类进行事件的注册与触发。
public sealed class EventHandlerList : IDisposable
{
    
// Fields
    private ListEntry head;
    
private Component parent;

    
// Methods
    public EventHandlerList();
    
internal EventHandlerList(Component parent)
    {
            
this.parent = parent;
    }

 
public void AddHandlers(EventHandlerList listToAddFrom)
{
    
for (ListEntry entry = listToAddFrom.head; entry != null; entry = entry.next)
    {
        
this.AddHandler(entry.key, entry.handler);
    }
}

private ListEntry Find(object key)
{
    ListEntry head 
= this.head;
    
while (head != null)
    {
        
if (head.key == key)  
        {
     
       return head;  这里使用递归找到这种 委托类型的 ListEntry
        }
        head 
= head.next;
    }
    
return head;
}

 
public void RemoveHandler(object key, Delegate value)
{
    ListEntry entry 
= this.Find(key);
    
if (entry != null)
    {
        entry.handler 
= Delegate.Remove(entry.handler, value);
    }
}

    
// Properties
    
public Delegate this[object key]
{
    
get
    {
        ListEntry entry 
= null;
        
if ((this.parent == null|| this.parent.CanRaiseEventsInternal)
        {
            entry 
= this.Find(key);
        }
        
if (entry != null)
        {
            
return entry.handler; //等于这种委托类型的委托,有可能是个handler链表
        }
        
return null;
    }
    
set
    {
        ListEntry entry 
= this.Find(key);
        
if (entry != null)
        {
            entry.handler 
= value;
        }
        
else
        {
            
this.head = new ListEntry(key, value, this.head);
        }
    }
}
 

 
public void AddHandler(object key, Delegate value)
{
    ListEntry entry 
= this.Find(key);
    
if (entry != null)
    {
   
     entry.handler = Delegate.Combine(entry.handler, value); //如果已经在链表中注册过这种类别的委托,则找到并且把新添加的委托实例加入进去
    }
    
else
    {
        
this.head = new ListEntry(key, value, this.head);//如果还没有在链表中注册过这种类别的委托,则添加到链表中
    }
}
Dkey2                          |        DKey1
D2DosomthingList      |        D1DosomthingList
next                              |        next

 D2的next指向后面的整个对象,D1的next为null                                   

 

    // Nested Types
    private sealed class ListEntry
    {
        
// Fields
        internal Delegate handler;
        
internal object key;
        
internal EventHandlerList.ListEntry next;

        
// Methods
       public ListEntry(object key, Delegate handler, EventHandlerList.ListEntry next)
    {
            
this.next = next;
            
this.key = key;
            
this.handler = handler;
    }

    }
}
 
 
举例:
 
 public event EventHandler DateChanged
        {
            add
            {
                
base.Events.AddHandler(DateChangedEvent, value);
            }
            remove
            {
                
base.Events.RemoveHandler(DateChangedEvent, value);
            }
        }



 
protected void OnDateChanged(EventArgs e)
        {
            
if (base.Events != null)
            {
                EventHandler handler1 
= (EventHandler)base.Events[DateChangedEvent];
                
if (handler1 != null)
                {
                    handler1(
this, e);
                }
            }
        }

private static readonly object DateChangedEvent;

DateChangedEvent 
= new object();

转载于:https://www.cnblogs.com/kasafuma/archive/2011/04/07/2008204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值