EventHandlerList类型内部存储示意图
EventHandlerList提供一个简单的委托列表,该类使用线性搜索算法在委托列表中查找项。线性搜索算法在处理大量项时是低效的。因此当列表较大时,查找项的速度会比较慢,我们经常使用的事件委托,就是通过这个类型存储和调用的。用反射工具查看其代码
这个类的结构比较简单,但理解起来却不那么容易,我画了一个图,用以辅助理解。
欢迎各位能给予补充或指正。本人不胜感激。
Code
[HostProtection(SecurityAction.LinkDemand, SharedState=true)]
public sealed class EventHandlerList : IDisposable
{
// Fields
private ListEntry head;
private Component parent;
// Methods
public EventHandlerList()
{
}
internal EventHandlerList(Component parent)
{
this.parent = parent;
}
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);
}
}
public void AddHandlers(EventHandlerList listToAddFrom)
{
for (ListEntry entry = listToAddFrom.head; entry != null; entry = entry.next)
{
this.AddHandler(entry.key, entry.handler);
}
}
public void Dispose()
{
this.head = null;
}
private ListEntry Find(object key)
{
ListEntry head = this.head;
while (head != null)
{
if (head.key == key)
{
return head;
}
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;
}
return null;
}
set
{
ListEntry entry = this.Find(key);
if (entry != null)
{
entry.handler = value;
}
else
{
this.head = new ListEntry(key, value, this.head);
}
}
}
// 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;
}
}
}
[HostProtection(SecurityAction.LinkDemand, SharedState=true)]
public sealed class EventHandlerList : IDisposable
{
// Fields
private ListEntry head;
private Component parent;
// Methods
public EventHandlerList()
{
}
internal EventHandlerList(Component parent)
{
this.parent = parent;
}
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);
}
}
public void AddHandlers(EventHandlerList listToAddFrom)
{
for (ListEntry entry = listToAddFrom.head; entry != null; entry = entry.next)
{
this.AddHandler(entry.key, entry.handler);
}
}
public void Dispose()
{
this.head = null;
}
private ListEntry Find(object key)
{
ListEntry head = this.head;
while (head != null)
{
if (head.key == key)
{
return head;
}
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;
}
return null;
}
set
{
ListEntry entry = this.Find(key);
if (entry != null)
{
entry.handler = value;
}
else
{
this.head = new ListEntry(key, value, this.head);
}
}
}
// 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;
}
}
}
这个类的结构比较简单,但理解起来却不那么容易,我画了一个图,用以辅助理解。
欢迎各位能给予补充或指正。本人不胜感激。