惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
T
Tailwind CSS Blog
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Heimdal Security Blog
博客园 - 【当耐特】
W
WeLiveSecurity
J
Java Code Geeks
Latest news
Latest news
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
博客园 - Franky
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
PCI Perspectives
PCI Perspectives
博客园_首页
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
P
Palo Alto Networks Blog
I
InfoQ
Security Latest
Security Latest
Hacker News: Ask HN
Hacker News: Ask HN
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
Help Net Security
Help Net Security
F
Full Disclosure
Cyberwarzone
Cyberwarzone
D
DataBreaches.Net
The Cloudflare Blog
S
Securelist
美团技术团队
C
Cybersecurity and Infrastructure Security Agency CISA
AI
AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events

博客园 - 小兔快跑

实现线程中的参数传递 利用rank() 和 dense_rank() 来实现分类排名 利用Relations实现多DataTable的聚合 Web应用中实现发送带附件的电子邮件 为什么这样的SQL会锁记录? 在Word里实现禁止复制和选定 [VS2005]解决“由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题” Word转pdf方法小结 原来数据库编程也可以这么美好! ASP.NET生成WORD文档服务器部署注意事项 ASP.NET:页面保存为WORD出现的问题! 动态绑定图片url:数据绑定出现的问题 .NET对Excel的读写 .NET中的CString知识点 水滴石穿之页面遮罩层实现、向window.open()打开的窗口POST数据 动态加载和使用类型 如何:使用反射将委托挂钩 SVG常见问题汇总 水滴石穿之子页面的滚动条设置 表格的固定高度宽度问题 复制带格式的文本
WinForm 控件的事件委托剖析
小兔快跑 · 2008-04-28 · via 博客园 - 小兔快跑

首先从controlInstance.Click事件开始. 用Reflector反编译System.Windows.Forms.Control类可以看到对Click事件的定义:

[System.Windows.Forms.SRCategory("CatAction"), System.Windows.Forms.SRDescription("ControlOnClickDescr")] 
        
public event EventHandler Click 
        

            add 
            

                
base.Events.AddHandler(Control.EventClick, value); 
            }
 
            remove 
            

                
base.Events.RemoveHandler(Control.EventClick, value); 
            }
 
        }
 

这里的Control.EventClick是一个只读的静态私有属性,它是以后事件查找委托的键(key),请记住这个.

private static readonly object EventClick = new object(); 

Control的Events属性是由System.ComponentModel.Component 继承而来,它是EventHandlerList的实例.

private EventHandlerList events; 
protected EventHandlerList Events 

      
get 
      

            
if (this.events == null
            

                  
this.events = new EventHandlerList(); 
            }
 
            
return this.events; 
      }
 
}
 


EventHandlerList类有三个重要的方法:
      public void AddHandler(object key, Delegate value);
      public void RemoveHandler(object key, Delegate value);
      private ListEntry Find(object key);

AddHandler的作用是插入一个键和一个委托类型的值, 插入之前通过Find方法检查一下同样的委托对象是否存在,如果存在,则合并; 如果不存在,以要插入的委托对象(value)为头.

public void AddHandler(object key, Delegate value) 

      EventHandlerList.ListEntry entry1 
= this.Find(key); 
      
if (entry1 != null
      

            entry1.handler 
= Delegate.Combine(entry1.handler, value); 
      }
 
      
else 
      

            
this.head = new EventHandlerList.ListEntry(key, value, this.head); 
      }
 
}
 

如果是一个按钮的Click事件,我们一般定义为:

button1.Click += new EventHandler(OnButtonClick); 
protected void OnButtonClick(object sender, System.EventArgs e) 

    
// 你的处理函数 
}
 

则通过了button1.Events.AddHandler(Control.EventClick,  EventHandler handler),而这个handler却是一个MulticastDelegate的实例。看MS公布的.net framework 部分源码就知道了:

// ==++== 
//  
//    
//    Copyright (c) 2002 Microsoft Corporation.  All rights reserved. 
//    
//    The use and distribution terms for this software are contained in the file 
//    named license.txt, which can be found in the root of this distribution. 
//    By using this software in any fashion, you are agreeing to be bound by the 
//    terms of this license. 
//    
//    You must not remove this notice, or any other, from this software. 
//    
//  
// ==--== 
namespace System 
     
    
using System; 
    
/**//// <include file='doc\EventHandler.uex' path='docs/doc[@for="EventHandler"]/*' /> 
     [Serializable()] 
    
public delegate void EventHandler(Object sender, EventArgs e);}
 

现在我们转到对委托(Delegate)和多播委托(MulticastDelegate)的研究了。
Delegate类已经封装好产生委托,消除委托和执行委法的方法,它是一个不能实例化的抽象类。但.net 的编译器支持由delegate定义的类型来实例化一个Delegate对象,它能让这个对象的执行委托方法像普通函数一样调用(具体的可以看C#高级编程里面的实例),所以很多时候,delegate类型会被认为是函数指针。 
Delegate还有两个很重要的方法,组合委托Combine和删除委托Remove。在单播委托Delegate中使用这组合委托方法会抛出多播不支持异常(MulticastNotSupportedException)。而使用删除委托方法时,如果这个单播委托和要删除的委托是同一个值时,则返回null,证明已经删除;如果不是,则原封不动返回原来的单播委托。
EventHandler实际上是一个多播委托实例,所以它支持组合委托和删除委托的方法。这个实例,实际上是一个委托实例链,它是这个链的链尾。每次像调用普通函数调用这个委托的时候,这个委托会执行完委托的代理函数,并查找链表中上一个委托实例,执行这个委托的代理函数,再查找链表中上上个委托实例,执行当前委托的代理函数。。。 一直到链表被遍历完。

protected override sealed Object DynamicInvokeImpl(Object[] args) 
        

            
if (_prev != null
                _prev.DynamicInvokeImpl(args); 
            
return base.DynamicInvokeImpl(args); 
        }
 

好了。那可以想象,一个用户点击按钮button1,首先执行的函数是OnClick函数

[EditorBrowsable(EditorBrowsableState.Advanced)] 
protected virtual void OnClick(EventArgs e) 

    
if (this.CanRaiseEvents) 
    

        EventHandler handler1 
= (EventHandler) base.Events[Control.EventClick]; 
        
if (handler1 != null
        

            handler1(
this, e); 
        }
 
    }
 
}