EventHandlerList
public sealed class EventHandlerList : IDisposable
{
// Fields
private ListEntry head;
private Component parent;
// Methods
public EventHandlerList();
internal EventHandlerList(Component parent)
{
this.parent = parent;
}
{
// 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);
}
}
{
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;
}
{
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
{
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);
}
}
}
{
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);//如果还没有在链表中注册过这种类别的委托,则添加到链表中
}
}
{
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();
{
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();