再谈CLR:事件定义
来源:互联网 发布:day one 2 mac 破解版 编辑:程序博客网 时间:2024/05/16 12:19
这是今天课堂上的一个话题。如何定义事件,这好像并不是问题,我们习惯性直接用下面的代码定义,不是吗?
class Customer { public event EventHandler NameChanging; }.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
这样做的话,大致是会怎么编译呢?
其实编译器会帮我们生成两个方法: add_NameChanging和remove_NameChanging
还会有一个字段,是EventHandler这个类型的 。
关键就在这里。它每次都会在创建对象的时候准备一个EventHandler,但问题是,并不是每个事件都会被用到。这就意味着,有一部分EventHandler其实是浪费的。
如果类型的事件很多,那么这个问题可能比较明显。
如何改进呢?
请参考下面的代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ComponentModel;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Employee e = new Employee(); e.NameEvent += new EventHandler(e_NameEvent); e.LNameEvent += new EventHandler(e_LNameEvent); e.Name = "ares"; e.LName = "chen"; Console.Read(); } static void e_LNameEvent(object sender, EventArgs e) { Employee emp = (Employee)sender; Console.WriteLine(emp.LName); } static void e_NameEvent(object sender, EventArgs e) { Employee emp = (Employee)sender; Console.WriteLine(emp.Name); } } class Employee { private string name; public string Name { get { return name; } set { name = value; if (list[nameEventKey] != null) list[nameEventKey].DynamicInvoke(new object[] { this, null }); } } private string lname; public string LName { get { return lname; } set { lname = value; if (list[lnameEventKey] != null) list[lnameEventKey].DynamicInvoke(new object[] { this, null }); } } private EventHandlerList list = new EventHandlerList(); private object nameEventKey=null; public event EventHandler NameEvent { add { if (nameEventKey == null) nameEventKey = new object(); list.AddHandler(nameEventKey, value); } remove { if (nameEventKey == null) nameEventKey = new object(); list.RemoveHandler(nameEventKey, value); } } private object lnameEventKey=null; public event EventHandler LNameEvent { add { if (lnameEventKey == null) lnameEventKey = new object(); list.AddHandler(lnameEventKey, value); } remove { if (lnameEventKey == null) lnameEventKey = new object(); list.RemoveHandler(lnameEventKey, value); } } }}.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- 再谈CLR:事件定义
- CLR.CTS,CLS定义
- 再谈CLR:CLR的版本
- C++/CLR ref class 属性定义
- 并发事件: 实现 CLR 异步编程模型
- 实现 CLR 异步编程模型 并发事件
- 《CLR via C#:框架设计》读书笔记 - 事件
- 《CLR via C#》读书笔记---11事件
- C#事件-定义事件
- CLR
- CLR
- CLR
- clr
- CLR
- CLR
- CLR
- CLR
- CLR
- 如何在代码中执行应用程序安全性检查(声明式和代码式)
- 如何查找某个特定证书
- 如何读取计算机上面所有的证书信息
- 再谈CLR:构造之谜
- 再谈CLR:关于类型的拷问
- 再谈CLR:事件定义
- 对称加密的例子(DES)——直接加密和解密字符串
- 对称加密的例子(DES)——针对文件进行加解密
- new修饰符之惑
- Convert XElement to XmlNode (and Convert XmlNode to XElement){转载}
- 安装电脑看PPS
- 使用BT3破解WEP密码详细教程
- 如何配置SQL2008,让其允许C#远程外部连接。
- 2010年10月20日