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

概述
     C#中的委托类似于C或C++中的函数指针。使用委托使程序员可以将方法引用封装在委托对象内。然后可以将该委托对象传递给可调用所引用方法的代码,而不必在编译时知道将调用哪个方法。与C或C++中的函数指针不同,委托是面向对象,而且是类型安全的。
C#中的“事件”是当对象发生某些事情时,类向该类的客户提供通知的一种方法。事件最常见的用途是用于图形用户界面;通常,表示界面中的控件的类具有一些事件,当用户对控件进行某些操作(如单击某个按钮)时,将通知这些事件。
使用委托来声明事件。委托对象封装一个方法,以便可以匿名调用该方法。事件是类允许客户为其提供方法(事件发生时应调用这些方法)的委托的一种方法。事件发生时,将调用其客户提供给它的委托。 注明:委托是对方法的包装 在不确定要调用什么方法时候而又不能用抽象或者多态实现的时候用委托。  
委托在Observer模式示例:
先创建PilotLamp.cs:

public interface PilotLamp { /// /// green light /// void TurnOn(); /// /// notice /// string Notice { get; set; } }
 
再创建DelegateEvent.cs:

public delegate void EventHandler();
再创建TrafficLight.cs:

public class TrafficLight : PilotLamp { public event EventHandler Notices; private string notice; #region GreenLight 成员 public void TurnOn() { if (Notices != null) Notices(); } public string Notice { get { return notice; } set { notice = value; } } #endregion }
 
 
再创建Driver.cs:

public class Driver { private string Name; private PilotLamp greenLight; public Driver(string name, PilotLamp greenLight) { this.Name = name; this.greenLight = greenLight; } public void GoLeft() { Console.WriteLine(string.Format("{1}司机,{0},请向左开车.", greenLight.Notice, Name)); } }
 
再创建Pedestrian.cs:

public class Pedestrian { private string Name; private PilotLamp greenLight; public Pedestrian(string name, PilotLamp greenLight) { this.Name = name; this.greenLight = greenLight; } public void GoThrough() { Console.WriteLine( string.Format("{0}同志,{1},请向前走.", Name, greenLight.Notice)); } }
 
 
最后再调用:

public partial class Run : Form { public Run() { InitializeComponent(); } private void btnRun_Click(object sender, EventArgs e) { //------------------------------------- TrafficLight trafficLight = new TrafficLight(); Driver driverOne = new Driver("张三", trafficLight); Driver driverTwo = new Driver("李四", trafficLight); Pedestrian pedestrianOne = new Pedestrian("王五", trafficLight); Pedestrian pedestrianTwo = new Pedestrian("麻六", trafficLight); trafficLight.Notices += new Observer.EventHandler(driverOne.GoLeft); trafficLight.Notices += new Observer.EventHandler(driverTwo.GoLeft); trafficLight.Notices += new Observer.EventHandler(pedestrianOne.GoThrough); trafficLight.Notices += new Observer.EventHandler(pedestrianTwo.GoThrough); trafficLight.Notice = "绿灯亮了."; trafficLight.TurnOn(); //------------------------------------- } }
 
输出时选控制台应用程序如图:
namespace DelegateAndEvent { class Program { static void Main(string[] args) { Publishser pub = new Publishser(); _disibledevent=> strlist = pub.DoSomething(); foreach (string result in strlist) Console.WriteLine(result); System.Threading.Thread.Sleep(5000); } } public delegate string GeneralEventHandler(object sender,EventArgs e); public class Publishser { public event GeneralEventHandler NumberChanged; public List DoSomething() { List strList = new List(); if (NumberChanged == null) return strList; Delegate[] generalEventHandlers = NumberChanged.GetInvocationList(); foreach (Delegate generalEventHandler in generalEventHandlers) { GeneralEventHandler mothed = (GeneralEventHandler)generalEventHandler; IAsyncResult result = mothed.BeginInvoke(this, EventArgs.Empty, null, null); string str = mothed.EndInvoke(result); strList.Add(str); } return strList; } } public class _disibledevent=>imageimageimageimageimage步步为营 C# 技术漫谈 五、事件与委托机制委托事件
 
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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
Tags:  深入委托和事件 委托与事件 委托和事件的区别 突发事件应急机制 委托事件

延伸阅读

最新评论

发表评论