Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement publish / subscribe mode based on custom event EventArgs in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Event parameters / / event parameters class CustomEventArgs:EventArgs {public CustomEventArgs (string message) {Message = message;} public string Message {get; set;}} II. Event publisher / / event publisher: definition and invocation of events, triggering events can also be written in this class Publisher {public event EventHandler CustomEvent Public void DoSomething () {/ / can write something else here before calling / / OnCustomEvent (new CustomEventArgs ("I am the event parameter")) Event calls are wrapped in protected virtual methods, allowing derived classes to rewrite the invocation behavior protected virtual void OnCustomEvent (CustomEventArgs e) {CustomEvent?.Invoke (this, e);}} III. Event subscribers / / event subscribers: event method writing and subscription function class Subscriber {private readonly string Str / the subscription action is going to be here, so it is necessary to send the publisher public Subscriber (string str,Publisher publisher) {Str = str; / / subscription event publisher.CustomEvent + = HanderCustomEvent } private void HanderCustomEvent (object sender, CustomEventArgs e) {/ / do event Console.WriteLine here ($"publisher: {sender.GetType ()}, subscriber: {Str}, parameter is: {e.Message}");}} / / event subscriber: event method writing and subscription function class Subscriber2 {private readonly string Str / the subscription action is going to be in here, so the publisher public Subscriber2 (string str, Publisher publisher) {Str = str; / / subscription event publisher.CustomEvent + = HanderCustomEvent should be sent. } private void HanderCustomEvent (object sender, CustomEventArgs e) {/ / do the event Console.WriteLine here ($"publisher: {sender.GetType ()}, subscriber: {Str}, parameter is: {e.Message}");}} IV. Call event static void Main (string [] args) {Publisher publisher = new Publisher () Subscriber subscriber1 = new Subscriber ("subscriber1", publisher); Subscriber2 subscriber2 = new Subscriber2 ("subscriber2", publisher); / / call the method that raises the event publisher.DoSomething (); Console.ReadKey ();} that's all for "how C# implements the publish / subscribe model based on custom event EventArgs". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report