EventHandlerList z
写一个类时,有时候会在同一个类上添加很多事件,事件很多的话,是不容易管理的,.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 z的更多相关文章
- 【Python】使用torrentParser1.03对多文件torrent的分析结果
Your environment has been set up for using Node.js 8.5.0 (x64) and npm. C:\Users\horn1>cd C:\User ...
- Android立体旋转动画实现与封装(支持以X、Y、Z三个轴为轴心旋转)
本文主要介绍Android立体旋转动画,或者3D旋转,下图是我自己实现的一个界面 立体旋转分为以下三种: 1. 以X轴为轴心旋转 2. 以Y轴为轴心旋转 3. 以Z轴为轴心旋转--这种等价于andro ...
- Z字形扫描(201412-2)
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- 【IOS】将一组包含中文的数据按照#ABC...Z✿分组
上一篇文章[IOS]模仿windowsphone列表索引控件YFMetroListBox里面 我们一步步的实现了WindowsPhone风格的索引. 但是有没有发现,如果你要实现按照字母排序,你还得自 ...
- Java 压缩/ 解压 .Z 文件
1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...
- 中文编程语言Z语言开源正式开源!!!
(Z语言基于.NET环境,源码中有很多高技术的代码,让更多的人知道对大家有会有很好的帮助,请管理员一点要批准放在首页) 本人实现的中文编程语言Z语言现在正式开源,采用LGPL协议. 编译器核心的网址为 ...
- 性能优化方法(Z)
关于C#程序优化的五十种方法 作者: 字体:[增加 减小] 类型:转载 时间:2013-09-12我要评论 这篇文章主要介绍了C#程序优化的五十个需要注意的地方,使用c#开发的朋友可以看下 一.用属性 ...
- CCF——Z字形扫描问题
试题编号: 201412-2 试题名称: Z字形扫描 时间限制: 2.0s 内存限制: 256.0MB 问题描述: 问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag ...
- Z.ExtensionMethods 一个强大的开源扩展库
今天有意的在博客园里面搜索了一下 Z.ExtensionMethods 这个扩展类库,确发现只搜到跟这个真正相关的才两篇博文而已,我都点进去看了一下,也都只是提到而已,没有专门介绍,才引起我写这篇文档 ...
随机推荐
- PHP 各种函数
usleep() 函数延迟代码执行若干微秒. unpack() 函数从二进制字符串对数据进行解包. uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID. time_sleep_unti ...
- ASP.NET输入文本框自动提示功能
在ASP.NET Web开发中会经常用到自动提示功能,比如百度搜索.我们只要输入相应的关键字,就可以自动得到相似搜索关键字的提示,方便我们快速的输入关键字进行查询. 那么在ASP.NET中,如果我们需 ...
- input效果:当鼠标在input中输入文字是改变内部文字效果
主要用到属性:onpropertychange事件(属性改变时触发的事件),oninput属性(当input有输入时发生的事件) onpropertychange事件是IE专属事件 oninput属性 ...
- 51nod贪心算法入门-----独木舟问题
独木舟问题 n个人,已知每个人体重,独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? 分析:按照 ...
- CRF图像语义分割
看了Ladicky的文章Associative Hierarchical CRFs for Object Class Image Segmentation,下载他主页的代码,文章是清楚了,但代码的RE ...
- 编译报错GLIBCXX_3.4.15 clock_gettime@@GLIBC_2.2
GLIBCXX_3.4.15 升级gcc,g++编译器 clock_gettime@@GLIBC_2.2 链接库时加-lrt
- Hive(转)
Hive分区表 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition概念.分区表指的是在创建表时指 ...
- canvas 乒乓球
<!DOCTYPE html> <html> <head> <title>Bouncing Ball With inputs</title> ...
- H5动画优化之路
H5动画60fps之路 在移动端,和Native相比,H5一直都被人吐槽性能差,尤其是在动画方面. 谈到整个Web app的生命周期,一般分为四个部分: 加载 等待用户 响应用户 动画 一般情况下,首 ...
- 自适应网页设计(Responsive Web Design)(转)
随着3G的普及,越来越多的人使用手机上网. 移动设备正超过桌面设备,成为访问互联网的最常见终端.于是,网页设计师不得不面对一个难题:如何才能在不同大小的设备上呈现同样的网页? 手机的屏幕比较小,宽度通 ...