This is very basic Event Bus implementation written in c#.
Create a new instance of an event bus.
Bus bus = new Bus();
To publish an event use post method like so:
bus.Post(new LanistersSlainedEvent(5));
You can use whatever object you wish as event parameter;
To subscribe to an event annotate a method with [Subscribe] annotation. Subscription method must return void and only use one parameter. Below you can see an example of an subscription method that listens for events that pass LanistersSlainedEvent parameter.
[Subscribe]
public void WhateverNameYouWant(LanistersSlainedEvent e)
{
//Do something
}
One more thing you have to do, for object to start listening for bus events is calling Register
method.
bus.Register(this);
When Register
is called, Bus will collect subscription functions and add them to its collection.
To cancel the subscriptions you must call Unregister
. You should also call Unregister
when disposing of class.
Solution also contains SimpleBusSample
which is a Windows Forms project that enables you to add/remove TestClass
objects, which subscribe to certain events, etc. All subscribe functions log to Console output.