我们写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.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,
}
}

用起来更方便,很难出错。希望对大家有用。

类型安全的EventHandlerList的更多相关文章

  1. EventHandlerList z

    写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.NET提供的EventHandlerList可以辅助多个事件的管 理,但不方便的地方是,它不是类型安全的,缺少类型安全,多 ...

  2. 04_Swift2基础之类型安全和类型推测+字面量+类型别名

    1. 类型安全和类型推测 1> 类型安全 Swift 是一个 _类型安全(type safe)_ 的语言.类型安全的语言可以让你清楚地知道代码要处理的值的类型.如果你的代码需要一个`String ...

  3. c++ 类型安全

    类型安全很大程度上可以等价于内存安全,类型安全的代码不会试图访问自己没被授权的内存区域.“类型安全”常被用来形容编程语言,其根据在于该门编程语言是否提供保障类型安全的机制:有的时候也用“类型安全”形容 ...

  4. 类型安全且自动管理内存的返回 std::string 的 sprintf 实现

    在这篇博文里,我提到了一个例子,说的是使用C++实现类型安全的printf.这个例子很惊艳,但是在我写程序的时候,并非那么"迫切"地需要它出现在我的工具箱中,因为它并不比普通的pr ...

  5. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  6. Swift编程语言学习1.3——类型安全和投机型

    Swift 是类型安全(type safe )语言.类型安全的语言可以让你清楚地知道代码被处理值类型.假设你需要一个代码String.你绝对不能进去一个不小心传球Int. 因为 Swift 它是类型安 ...

  7. Matlab与.NET基于类型安全的接口混合编程入门

    原文:[原创]Matlab与.NET基于类型安全的接口混合编程入门 如果这些文章对你有用,有帮助,期待更多开源组件介绍,请不要吝啬手中的鼠标. [原创分享]Matlab.NET混编调用Figure窗体 ...

  8. Swift语言指南(四)--类型安全和类型推断

    原文:Swift语言指南(四)--类型安全和类型推断 Swift是一门类型安全语言,类型安全语言需要代码里值的类型非常明确.如果你的代码中有部分值需要String类型,你就不能错误地传递Int. 鉴于 ...

  9. 自动类型安全的.NET标准REST库refit

    在SCOTT HANSELMAN 博客上看到一个好东西<Exploring refit, an automatic type-safe REST library for .NET Standar ...

随机推荐

  1. 【数组】word search

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  2. 关于dubbo调度时出现Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method insertTestTb in the service cn.cuibusi.core.service.TestTbService.的解决办法

    在用dubbo跨项目调度service时出现如下错误: 错误原因:pojo没有实现序列化 解决方法:在pojo实现序列化接口即可

  3. MYSQL数据库的日志文件

    日志文件:用来记录MySQL实例对某种条件做出响应时写入的文件.如错误日志文件.二进制日志文件.慢查询日志文件.查询日志文件等. 错误日志 show variables like 'log_error ...

  4. R语言统计字符串的字符数ncahr函数

    函数计算字符数量,包括在一个字符串的空格的个数. 语法 nchar()函数的基本语法是: nchar(x) 以下是所使用的参数的说明: x - 向量输入. 示例 result <- nchar( ...

  5. JAVA练手--线程(Thread)

    1. 查看线程是否还存活 package tet;public class kk extends Thread{ //1. 查看线程是否还存活 public void run(){ for(int i ...

  6. handler与anr机制

    1. handler 参考资料:http://blog.csdn.net/ly502541243/article/details/52062179/ 首先说明Android的两个特性: 1. 只能在主 ...

  7. SQL Server 两个时间段的差and时间截取到时分

    /*--------------------------时间截取到时分-----------------------------------*/ ), ),)--返回2017-11-24 19:25 ...

  8. C# 之正则表达式运用

    C#正则验证大全 Regex.IsMatch()正则表达式验证 需要引入命名空间 using System.Text.RegularExpressions;    #region 验证文本框输入为数字 ...

  9. js画一棵树

    用纯js画一棵树.思路: 1.一棵树的图片,作为页面背景: 2.通过html5中的canvas画布进行遮罩: 3.定时每隔10ms,从下往上清除1px的遮罩: <!DOCTYPE html> ...

  10. Java的文档注释之生成帮助文档

    示例: /** * Title: Person类<br/> * Description:通过Person类说明Java中的文档注释<br/> * Company: *** * ...