我們寫一個類時有時候會在同一個類上添加很多事件事件很多的話是不容易管理的NET提供的EventHandlerList可以輔助多個事件的管理但不方便的地方是它不是類型安全的缺少類型安全多少用起來擔心會出錯經過我的一番改造可以將系統提供的EventHandlerList通過泛型提供類型安全的管理泛型類EventHandlerListcs的實現如下
public sealed class EventHandlerList<T> : IDisposable
{
ListEntry head;
public EventHandlerList()
{ }
public Delegate this[T key]
{
get{
ListEntry e = Find(key);
return e == null ? null : em_handler;
}
set{
ListEntry e = Find(key);
if (e != null){
em_handler = value;
}
else {
head = new ListEntry(key value head);
}
}
}
public void AddHandler(T key Delegate value)
{
ListEntry e = Find(key);
if (e != null) {
em_handler = DelegateCombine(em_handler value);
}
else {
head = new ListEntry(key value head);
}
}
public void AddHandlers(EventHandlerList<T> listToAddFrom)
{
ListEntry currentListEntry = listToAddFromhead;
while (currentListEntry != null) {
AddHandler(currentListEntrym_key currentListEntrym_handler);
currentListEntry = currentListEntrym_next;
}
}
public void Dispose()
{
head = null;
}
private ListEntry Find(T key)
{
ListEntry found = head;
while (found != null) {
if (foundm_keyEquals(key)) {
break;
}
found = foundm_next;
}
return found;
}
public void RemoveHandler(T key Delegate value)
{
ListEntry e = Find(key);
if (e != null) {
em_handler = DelegateRemove(em_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 {
EventsAddHandler(EventTypeReceiveData value);
}
remove {
EventsRemoveHandler(EventTypeReceiveData value);
}
}
public event EventHandler OnRefreshData
{
add{
EventsAddHandler(EventTypeRefreshData value);
}
remove{
EventsRemoveHandler(EventTypeRefreshData 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
}
}
用起來更方便很難出錯希望對大家有用
From:http://tw.wingwit.com/Article/program/Web/201404/30637.html