.NET漫游指南-003-事件

来源:互联网 发布:火星时代有网络班 编辑:程序博客网 时间:2024/05/17 00:08

下面所有的都离不开这句话:事件基于委托,所以要先弄清委托再去研究事件。
事件基于委托,为委托提供了一种 发布/订阅机制。
关键字:event
作为约定,事件一般使用带两个参数的方法,其中第一个参数是一个对象,包含事件的发送者。第二个参数提供了事件的相关信息。
例如 EventHandler<TEventArgs> 的第一个参数必须是object类型,第二个参数是T类型。其中对T类型的参数做了约束:必须是派生自EventArgs。从EventHandler<TEventArgs> 定义可以看出

    public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e)where TEventArgs:EventArgs

下面将通过一个例子来讲述事件中委托订阅-发布。
故事背景:消费者要买车,如果经销商手里有新车时就通知有购买意愿的消费者,消费者接收到订阅的消息后做出相应的反应。
例子中有三个类:
CarDeal.cs类=》这个类中包含了事件中的相关信息(派生自EventArgs),事件的触发函数。
Consumer.cs类=》这个类扮演的角色就时事件的订阅者,当事件被触发时,事件的订阅者就会被通知,同时事件信息也会传给订阅者,订阅者根据事件信息进行相应的逻辑处理。
program.cs类=》来实现发布者和订阅者的实例化,并调用函数,来触发事件。
源码地址:http://download.csdn.net/download/geshicuowu/9935237

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/// <summary>/// 这个是car的处理类/// </summary>namespace EventExample{    //这是为符合EventHandler<>使用规范而定义的类,它传递事件中的信息    public class CarInforEventArgs : EventArgs    {        public CarInforEventArgs(string car)        {            this.Car = car;        }        public string Car        {            get;            private set;        }    }    class CarDeal    {        //声明一个事件         public event EventHandler<CarInforEventArgs> newCarInfo;        //当实例化该方法时,就相当于触发了事件(因为里面有newCarInfo事件发布函数)         public void NewCar(string newCar)        {            Console.WriteLine("car dealer : new car {0}", newCar);            //检查该事件是否被订阅            if(newCarInfo != null)            {                //触发事件,在这里是通过NewCar()方法来触发事件的                //就如同你点击一个按钮时触发的click事件,原理时一样的                newCarInfo(this, new CarInforEventArgs(newCar));            }        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/// <summary>/// 这个类在整个事件中作为事件的侦听器/// 订阅CarDeal类的事件/// 并定义了一个方法NewCarIsHere,该方法满足EventHandler<CarInfoEventArgs>委托的要求/// 只有满足委托要求,才能将NewCarIsHere的方法进行委托,并获取事件中的信息。/// </summary>namespace EventExample{    class Consumer    {        private string _name;        public Consumer(string name)        {            _name = name;        }        public void NewCarWanted(object obj , CarInforEventArgs e)        {            Console.WriteLine("{0}:new car {1} is I want .", _name, e.Car);        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;/// <summary>/// 这个项目是来演示事件的相关理论/// author:zhangzhen/// date   :2017/08/14/// </summary>namespace EventExample{    class Program    {        static void Main(string[] args)        {            //实例化事件            var carDeal = new CarDeal();            var zhangZhen = new Consumer("zhangZhen");            //进行事件的委托订阅,当事件被触发时(在此处是触发carDeal中的NewCar方法)就运行被委托的方法            //并将事件信息传递给被委托的事件            carDeal.newCarInfo += zhangZhen.NewCarWanted;            carDeal.NewCar("奔驰");            var xiaoLi = new Consumer("xiaoLi");            carDeal.newCarInfo += xiaoLi.NewCarWanted;            carDeal.NewCar("马丁斯顿");            //将委托事件xiaoLi.NewCarIsHere从委托链表中移除,也就是取消订阅,当有事件发生时,不再通知该方法。            carDeal.newCarInfo -= xiaoLi.NewCarWanted;            carDeal.NewCar("劳斯莱斯");            //等待键盘输入            Console.ReadKey();        }    }}

程序可正常运行,并得出预期结果。
<欢迎讨论>

原创粉丝点击