C# Messaging

There isn’t really an intuitive way of messaging between classes inside of a c# project. One method involves keeping references to other classes and then deliberately calling methods inside of that other class. This becomes a tight coupling scheme that can break easily with project complexity and multiple listeners. I wanted to find something that could message and use delegates similar to other languages. Different platforms have messaging or publisher systems to pass around events where other other classes simply need to subscribe to the event. I found a decent solution that utilizes Window’s built-in System events inspired from this site.

Messaging Class
We can use the standard System.EventArgs and subclass it to contain simpler message content: a string.

Messenger Class
The Messenger class is able to fire off MessageEventArgs events to the system using the standard System.EventHandler.

Receiving Class
The receiving class simply needs to register as a listener and perform some work:

Comments are closed.



  • slide
  • slide
  • slide
  • slide