.net 系列:事件和委托

在.net 的世界里,离不开委托和事件,其实理解透了后很简单,总结了一下分为6步:
 
1)定义委托
 
public delegate void RevicedEventHandler(object sender,RevicedEventArgs e);
 
 RevicedEventArgs :自定义参数源,继承至EventArgs类。
 
2) 定义事件
public event RevicedEventHandler RevicedEvent;
 
3) 定义触发事件的函数
public virtual  void OnReviced(RevicedEventArgs  e)
{
   if(RevicedEvent!=null)
   {
       RevicedEvent(this,e);
   }
}
 
4)定义事件处理函数
public void DoSome(object sender,RevicedEventArgs  e)
{
   .......
}
 
5) 注册事件处理函数(将事件处理函数和事件关联起来)
 
 this.RevicedEvent+=new RevicedEvent(DoSome);
 
6)调用事件,满足特定条件去调用
    
person p =new person();
p.OnReviced(new RevicedEventArgs ());
 
 
说明:第1、2、3步是在发布者类定义,4、5步是在订阅类定义
 
其实:.net 后续又提供了泛型版本委托:public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e);
所以,第2步的事件可以换成:public event EventHandler<RevicedEventArgs  >RevicedEvent;此时第1步可以省略掉。
 
 
下面我们就举个实际的例子说明
比如我需要租一间小型房间,而我没有时间自己去找,于是我到中介委托帮我找,找到我需要的房间(小户型)的房间后通知我。
角色:我(Subscriber订阅者)  中介(publisher发布者
业务逻辑:中介将我的小型户型需求发布到房源池中(挂牌),而我订阅了我委托给中介的小房间需求的事件,当中介找到符合我房源后,就通知我。
 
 
 1  /// <summary>
 2     /// 发布者(中介)
 3     /// </summary>
 4     public class Publisher
 5     {
 6 
 7 
 8         public delegate void FindedSmallHouseEventHandler(object sender, HouseEventArgs e);//定义找小房子的委托
 9 
10 
11         /// <summary>
12         /// 定义找小房子事件
13         /// </summary>
14         public event FindedSmallHouseEventHandler FindedSmallHouseEvent;
15 
16         /// <summary>
17         /// (对内)定义触发事件的函数
18         /// </summary>
19         /// <param name="e"></param>
20         protected virtual void OnFindedSmallHouse(HouseEventArgs e)
21         {
22             if (null != FindedSmallHouseEvent)
23             {
24                 if (e.HouseType == HouseType.Small)//如果是小户型,才会触发事件,本人是屌丝,哈哈
25                 {
26                     FindedSmallHouseEvent(this, e);
27                 }
28 
29             }
30         }
31 
32         /// <summary>
33         /// 对外部类公开触发事件的函数
34         /// </summary>
35         public void StartFindHouse(HouseEventArgs e)
36         {
37             OnFindedSmallHouse(e);
38         }
39     }
 1  /// <summary>
 2     /// 订阅者(我自己)
 3     /// </summary>
 4     public class Subscriber
 5     {
 6         
 7         public Subscriber(Publisher p)
 8         {
 9             p.FindedSmallHouseEvent += new FindedSmallHouseEventHandler(ReceivedSmallHouse);
10 
11         }
12 
13       
14 
15         /// <summary>
16         /// 定义找到小房子的事件处理程序(接受小房子)
17         /// </summary>
18         /// <param name="sender"></param>
19         /// <param name="e"></param>
20         public void ReceivedSmallHouse(object sender, HouseEventArgs e)
21         {
22             Console.WriteLine("我终于有房子了,再也不用流落街头了。");
23             Console.ReadKey();
24         }
25     }
 1  /// <summary>
 2     /// 事件参数
 3     /// </summary>
 4     public class HouseEventArgs : EventArgs
 5     {
 6         public HouseEventArgs(HouseType H)
 7         {
 8             HouseType = H;
 9         }
10 
11 
12         public HouseType HouseType { get; set; }
13     }
 1  /// <summary>
 2     /// 房型
 3     /// </summary>
 4     public enum HouseType
 5     {
 6         /// <summary>
 7         /// 小户型(屌丝)
 8         /// </summary>
 9         Small = 0,
10         /// <summary>
11         /// 一般户型(中产阶级)
12         /// </summary>
13         Normal,
14         /// <summary>
15         /// 大户型(土豪)
16         /// </summary>
17         Big
18     }

调用:

 1 class Program
 2     {
 3         /// <summary>
 4         /// 按满足指定条件去调用事件(此场景就好比中介不停在找房子)
 5         /// </summary>
 6         /// <param name="args"></param>
 7         static void Main(string[] args)
 8         {
 9            
10 
11             Publisher pub = new Publisher();//中介实例
12             Subscriber sb = new Subscriber(pub);//我自己
13           
14             int i = int.MinValue;
15             bool f = true;
16             while (f)
17             {
18                 Console.WriteLine("请输入您要找的房型!");
19 
20                 if (int.TryParse(Console.ReadLine().ToString(), out i))
21                 {
22                     if (0 <= i && i <= 2) //定义了3种房型(小、一般、大户型)
23                     {
24                         HouseEventArgs eg = new HouseEventArgs((HouseType)i);
25                         pub.StartFindHouse(eg); //中介开始找房子
26                     }
27 
28 
29                 }
30 
31 
32             }
33 
34         }
35 
36     }

 运行结果:

例子写完了,有不好的地方欢迎指出!!!

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