.NET中的事件模式是如何工作的?


事件是一种使用委托的简化模式。在C#中,所有委托都具有多播能力,即委托的实例不仅可以表示单个方法,还可以表示方法列表。例如:

示例

delegate int Transformer(int x);
static void Main(){
   Transformer perform = x =>{
      int result = x * x;
      Console.WriteLine(result);
      return result;
   };
   perform += x =>{
      int result = x * x * x;
      Console.WriteLine(result);
      return result;
   };
   perform(2); // prints 4 and 8
}

调用Perform()会调用添加到委托中的所有方法。方法按照添加的顺序调用。

这允许你实现发布-订阅模式,也称为事件模式。发布者包含一个委托字段。它决定何时通过调用委托来发布。订阅者将自己的方法添加到发布者的委托中,因此每当发布者决定通过调用委托来发布时,它们都会收到通知。订阅者不知道也不干涉其他订阅者。

C#事件是一种语言特性,它以类型安全的方式规范了上述模式,从而避免了委托的常见错误。事件公开实现事件模式所需的委托功能的子集。下面的示例对此进行了说明。

首先,定义事件发生时要发送的数据结构。

public class ThresholdReachedEventArgs : EventArgs{
   public int Threshold { get; set; }
   public DateTime TimeReached { get; set; }
}

然后,定义发布者,即想要引发事件的类。

public class Counter{
   // 1. Define an event
   public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
   // 2. Do something that raises the event, and pass the EventArgs custom data
   public void DoSomething(){
      ThresholdReachedEventArgs e = new ThresholdReachedEventArgs{
         Threshold = 10,
         TimeReached = DateTime.Now
      };
      // 3. Raise the actual event
      ThresholdReached?.Invoke(this, e);
   }
}

最后,创建一个或多个订阅者。想要监听发布者引发的事件的类。

class Program{
   static void Main(string[] args){
      // 1. Create an instance of the publisher, which you want to listen to
      Counter counter = new Counter();
      // 2. Attach an event handler on the publisher
      counter.ThresholdReached += OnThresholdReached;
      // 3. Do something that will raise the event. Now you are ready to listen to the event.
      counter.DoSomething();
   }
   // 4. Handle the event which is raised by publisher
   static void OnThresholdReached(object sender, ThresholdReachedEventArgs e){
      Console.WriteLine($"Reached Threshold {e.Threshold} at {e.TimeReached.ToString()}");
   }
}

示例

在线演示

using System;
class Program{
   static void Main(string[] args){
      // 1. Create an instance of the publisher, which you want to listen to
      Counter counter = new Counter();

      // 2. Attach an event handler on the publisher
      counter.ThresholdReached += OnThresholdReached;

      // 3. Do something that will raise the event. Now you are ready to listen to the       event.
      counter.DoSomething();
   }

   // 4. Handle the event which is raised by publisher
   static void OnThresholdReached(object sender, ThresholdReachedEventArgs e){
      Console.WriteLine($"Reached Threshold {e.Threshold} at {e.TimeReached.ToString()}");
   }
}
public class ThresholdReachedEventArgs : EventArgs{
   public int Threshold { get; set; }
   public DateTime TimeReached { get; set; }
}
public class Counter{
   // 1. Define an event
   public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
   // 2. Do something that raises the event, and pass the EventArgs custom data
   public void DoSomething(){
      ThresholdReachedEventArgs e = new ThresholdReachedEventArgs{
         Threshold = 10,
         TimeReached = DateTime.Now
      };
      // 3. Raise the actual event
      ThresholdReached?.Invoke(this, e);
   }
}

输出

Reached Threshold 10 at 5/15/2021 12:49:10 PM

更新于:2021年5月19日

475 次浏览

启动你的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.