关键字: Delegate, MulticastDelegate, EventHandler, EventHandlerList, EventHandlerList.ListEntry, Control, Component 首先从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); } } }
handler1就是一个多播委托, 如果不为空,则执行它,而且这个执行就是执行所有的代理函数。这样就明白了WinForm控件Click事件所有的始终了!
参考文章:
http://blog.sunmast.com/Sunmast/archive/2005/04/21/1769.aspx
以上是个人观点,由于时间仓促及个人水平有限,难免错误,敬请斧正!