Fork me on GitHub
步步为营 C# 技术漫谈 五、事件与委托机制

步步为营 C# 技术漫谈 五、事件与委托机制

2011-06-20 22:25 by spring yang, 555 visits, 收藏, 编辑

概述

     C#中的委托类似于C或C++中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C++中的函数指针不同,委托是面向对象,而且是类型安全的。

C#中的“事件”是当对象发生某些事情时,类向该类的客户提供通知的一种方法。事件最常见的用途是用于图形用户界面;通常,表示界面中的控件的类具有一些事件,当用户对控件进行某些操作(如单击某个按钮)时,将通知这些事件。

使用委托来声明事件。委托对象封装一个方法,以便可以匿名调用该方法。事件是类允许客户为其提供方法(事件发生时应调用这些方法)的委托的一种方法。事件发生时,将调用其客户提供给它的委托。
注明:委托是对方法的包装 在不确定要调用什么方法时候而又不能用抽象或者多态实现的时候用委托。

委托在Observer模式示例:

先创建PilotLamp.cs:

01 public interface PilotLamp
02 {
03     /// <summary>
04     /// green light
05     /// </summary>
06     void TurnOn();
07  
08     /// <summary>
09     /// notice
10     /// </summary>
11     string Notice
12     {
13         get;
14         set;
15     }
16 }

再创建DelegateEvent.cs:

1 public delegate void  EventHandler();

再创建TrafficLight.cs:

01 public class TrafficLight : PilotLamp
02 {
03     public event EventHandler Notices;
04     private string notice;
05  
06     #region GreenLight 成员
07  
08     public void TurnOn()
09     {
10         if (Notices != null)
11             Notices();
12     }
13  
14     public string Notice
15     {
16         get
17         {
18             return notice;
19         }
20         set
21         {
22             notice = value;
23         }
24     }
25  
26     #endregion
27 }

再创建Driver.cs:

01 public class Driver
02 {
03     private string Name;
04     private PilotLamp greenLight;
05  
06     public Driver(string name, PilotLamp greenLight)
07     {
08         this.Name = name;
09         this.greenLight = greenLight;
10     }
11  
12     public void GoLeft()
13     {
14         Console.WriteLine(string.Format("{1}司机,{0},请向左开车.", greenLight.Notice, Name));
15     }
16 }

再创建Pedestrian.cs:

01 public  class Pedestrian
02 {
03     private string Name;
04     private PilotLamp greenLight;
05     public Pedestrian(string name, PilotLamp greenLight)
06     {
07         this.Name = name;
08         this.greenLight = greenLight;
09     }
10     public void  GoThrough()
11     {
12         Console.WriteLine(  string.Format("{0}同志,{1},请向前走.", Name, greenLight.Notice));
13     }
14 }

最后再调用:

01 public partial class Run : Form
02 {
03     public Run()
04     {
05         InitializeComponent();
06     }
07  
08     private void btnRun_Click(object sender, EventArgs e)
09     {
10         //-------------------------------------
11  
12  
13         TrafficLight trafficLight = new TrafficLight();
14  
15         Driver driverOne = new Driver("张三", trafficLight);
16         Driver driverTwo = new Driver("李四", trafficLight);
17  
18         Pedestrian pedestrianOne = new Pedestrian("王五", trafficLight);
19         Pedestrian pedestrianTwo = new Pedestrian("麻六", trafficLight);
20  
21         trafficLight.Notices += new Observer.EventHandler(driverOne.GoLeft);
22         trafficLight.Notices += new Observer.EventHandler(driverTwo.GoLeft);
23         trafficLight.Notices += new Observer.EventHandler(pedestrianOne.GoThrough);
24         trafficLight.Notices += new Observer.EventHandler(pedestrianTwo.GoThrough);
25         trafficLight.Notice = "绿灯亮了.";
26         trafficLight.TurnOn();
27  
28         //-------------------------------------
29  
30     }
31  
32 }

输出时选控制台应用程序如图:

image

结果如下图:

image

事件的使用示例:

01 namespace DelegateAndEvent
02 {
03     class Program
04     {
05         static void Main(string[] args)
06         {
07             Publishser pub = new Publishser();
08             OneScriber oneSub = new OneScriber();
09             TwoScriber twoSub = new TwoScriber();
10             ThreeScriber threeSub = new ThreeScriber ();
11             pub.NumberChanged += new GeneralEventHandler(oneSub.OnNumberChanged);
12             pub.NumberChanged += new GeneralEventHandler(twoSub.OnNumberChanged);
13             pub.NumberChanged += new GeneralEventHandler(threeSub.OnNumberChanged);
14             pub.DoSomething();
15         }
16     }
17  
18     public delegate string GeneralEventHandler();
19  
20     public class Publishser
21     {
22         public event GeneralEventHandler NumberChanged;
23  
24         public void DoSomething()
25         {
26             if (NumberChanged != null)
27             {
28                 Delegate[] generalEventHandlers = NumberChanged.GetInvocationList();
29                 foreach (Delegate generalEventHandler in generalEventHandlers)
30                 {
31                     GeneralEventHandler mothed = (GeneralEventHandler)generalEventHandler;
32                     string rtn = mothed();
33                     Console.WriteLine(rtn);
34                     System.Threading.Thread.Sleep(2000);
35                 }
36             }
37         }
38     }
39  
40     public class OneScriber
41     {
42         public string OnNumberChanged()
43         {
44             return "One Subscriber";
45         }
46     }
47  
48     public class TwoScriber
49     {
50         public string OnNumberChanged()
51         {
52             return "Two Subscriber";
53         }
54     }
55  
56     public class ThreeScriber
57     {
58         public string OnNumberChanged()
59         {
60             return "Three Subscriber";
61         }
62     }
63 }

运行结果:

image

注意到Delegate是GeneralEventHandler 的基类,所以为了触发事件,先要进行一个向下的强制转换,之后才能在其上触发事件,调用所有注册对象的方法。除了使用这种方式以外,还有一种更灵活方式可以调用方法,它是定义在Delegate基类中的DynamicInvoke()方法:

public object DynamicInvoke(params object[] args);

这可能是调用委托最通用的方法了,适用于所有类型的委托。它接受的参数为object[],也就是说它可以将任意数量的任意类型作为参数,并返回单个object对象。上面的DoSomething()方法也可以改写成下面这种通用形式:

代码作如下改动:

01 namespace DelegateAndEvent
02 {
03     class Program
04     {
05         static void Main(string[] args)
06         {
07             Publishser pub = new Publishser();
08             OneScriber oneSub = new OneScriber();
09             TwoScriber twoSub = new TwoScriber();
10             ThreeScriber threeSub = new ThreeScriber();
11             pub.NumberChanged += new GeneralEventHandler(oneSub.OnNumberChanged);
12             pub.NumberChanged += new GeneralEventHandler(twoSub.OnNumberChanged);
13             pub.NumberChanged += new GeneralEventHandler(threeSub.OnNumberChanged);
14             List<string> strlist = pub.DoSomething();
15             foreach (string result in strlist)
16                 Console.WriteLine(result);
17             System.Threading.Thread.Sleep(5000);
18         }
19     }
20  
21     public delegate string GeneralEventHandler();
22  
23     public class Publishser
24     {
25         public event GeneralEventHandler NumberChanged;
26  
27         public List<string> DoSomething()
28         {
29             List<string> strList = new List<string>();
30             if (NumberChanged == null) return strList;
31  
32  
33             Delegate[] generalEventHandlers = NumberChanged.GetInvocationList();
34             foreach (Delegate generalEventHandler in generalEventHandlers)
35             {
36                // GeneralEventHandler mothed = (GeneralEventHandler)generalEventHandler;
37                 string  rtn = generalEventHandler.DynamicInvoke(null).ToString();
38                 strList.Add(rtn);
39                
40             }
41             return strList;
42         }
43     }
44  
45     public class OneScriber
46     {
47         public string OnNumberChanged()
48         {
49             return "One Subscriber";
50         }
51     }
52  
53     public class TwoScriber
54     {
55         public string OnNumberChanged()
56         {
57             return "Two Subscriber";
58         }
59     }
60  
61     public class ThreeScriber
62     {
63         public string OnNumberChanged()
64         {
65             return "Three Subscriber";
66         }
67     }
68 }

结果如下:

image

还是一样的结果.

委托的定义会生成继承自MulticastDelegate的完整的类,其中包含Invoke()、BeginInvoke()和 EndInvoke()方法。当我们直接调用委托时,实际上是调用了Invoke()方法,它会中断调用它的客户端,然后在客户端线程上执行所有订阅者的 方法(客户端无法继续执行后面代码),最后将控制权返回客户端。注意到BeginInvoke()、EndInvoke()方法,在.Net中,异步执行 的方法通常都会配对出现,并且以Begin和End作为方法的开头(最常见的可能就是Stream类的BeginRead()和EndRead()方法 了)。它们用于方法的异步执行,即是在调用BeginInvoke()之后,客户端从线程池中抓取一个闲置线程,然后交由这个线程去执行订阅者的方法,而 客户端线程则可以继续执行下面的代码。

BeginInvoke()接受“动态”的参数个数和类型,为什么说“动态”的呢?因为它的参数是在编译时根据委托的定义动态生成的,其中前面参数 的个数和类型与委托定义中接受的参数个数和类型相同,最后两个参数分别是AsyncCallback和Object类型,对于它们更具体的内容,可以参见 下一节委托和方法的异步调用部分。现在,我们仅需要对这两个参数传入null就可以了。另外还需要注意几点:

  • 在委托类型上调用BeginInvoke()时,此委托对象只能包含一个目标方法,所以对于多个订阅者注册的情况,必须使用 GetInvocationList()获得所有委托对象,然后遍历它们,分别在其上调用BeginInvoke()方法。如果直接在委托上调用 BeginInvoke(),会抛出异常,提示“委托只能包含一个目标方法”。
  • 如果订阅者的方法抛出异常,.NET会捕捉到它,但是只有在调用EndInvoke()的时候,才会将异常重新抛出。而在本例中,我们不使用 EndInvoke()(因为我们不关心订阅者的执行情况),所以我们无需处理异常,因为即使抛出异常,也是在另一个线程上,不会影响到客户端线程(客户 端甚至不知道订阅者发生了异常,这有时是好事有时是坏事)。
  • BeginInvoke()方法属于委托定义所生成的类,它既不属于MulticastDelegate也不属于Delegate基类, 我们需要进行一个向下转换,来获取到实际的委托类型。

示例:

01 namespace DelegateAndEvent
02 {
03     class Program
04     {
05         static void Main(string[] args)
06         {
07             Publishser pub = new Publishser();
08             OneScriber oneSub = new OneScriber();
09             TwoScriber twoSub = new TwoScriber();
10             ThreeScriber threeSub = new ThreeScriber();
11             pub.NumberChanged += new GeneralEventHandler(oneSub.OnNumberChanged);
12             pub.NumberChanged += new GeneralEventHandler(twoSub.OnNumberChanged);
13             pub.NumberChanged += new GeneralEventHandler(threeSub.OnNumberChanged);
14             List<string> strlist = pub.DoSomething();
15             foreach (string result in strlist)
16                 Console.WriteLine(result);
17             System.Threading.Thread.Sleep(5000);
18         }
19     }
20  
21     public delegate string GeneralEventHandler(object sender,EventArgs e);
22  
23     public class Publishser
24     {
25         public event GeneralEventHandler NumberChanged;
26  
27         public List<string> DoSomething()
28         {
29             List<string> strList = new List<string>();
30             if (NumberChanged == null) return strList;
31  
32  
33             Delegate[] generalEventHandlers = NumberChanged.GetInvocationList();
34             foreach (Delegate generalEventHandler in generalEventHandlers)
35             {
36                 GeneralEventHandler mothed = (GeneralEventHandler)generalEventHandler;
37                 IAsyncResult result = mothed.BeginInvoke(this, EventArgs.Empty, null, null);
38                 string str = mothed.EndInvoke(result);
39                 strList.Add(str);
40                
41             }
42             return strList;
43         }
44     }
45  
46     public class OneScriber
47     {
48         public string OnNumberChanged(object sender,EventArgs e)
49         {
50             return "One Subscriber";
51         }
52     }
53  
54     public class TwoScriber
55     {
56         public string OnNumberChanged(object sender, EventArgs e)
57         {
58             return "Two Subscriber";
59         }
60     }
61  
62     public class ThreeScriber
63     {
64         public string OnNumberChanged(object sender, EventArgs e)
65         {
66             return "Three Subscriber";
67         }
68     }
69 }

结果:

image

BeginInvoke的另外两个参数分别是AsyncCallback和Object类型,其中AsyncCallback是一个委托类型,它用于方法的回调,即是说当异步方法执行完毕时自动进行调用的方法。它的定义为:

public delegate void AsyncCallback(IAsyncResult ar);

Object类型用于传递任何你想要的数值,它可以通过IAsyncResult的AsyncState属性获得。

步步为营 C# 技术漫谈系列
步步为营 C# 技术漫谈 一、反射机制
步步为营 C# 技术漫谈 二、ASP.NET 页生命周期
步步为营 C# 技术漫谈 三、公共语言运行库(CLR)
步步为营 C# 技术漫谈 四、垃圾回收机制(GC)

作者:spring yang

出处:http://www.cnblogs.com/springyangwc/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted on 2011-06-21 09:26  HackerVirus  阅读(215)  评论(0编辑  收藏  举报