Evaluate my implementation of SignalBus / EventBus

Joined
Apr 19, 2020
Messages
7
Programming Experience
1-3
How do you like implementation through generic static class?

How optimised and convenient is this option?

I see the downside of calling Fire, we don’t know exactly what the arguments are, but we can look at delegate. Can we fix this?

C#:
public static class Test<T> where T : Delegate
{

    private static T _delegate;

    public static void Fire(params object[] args) => _delegate?.DynamicInvoke(args);
    public static void Subscribe(T action) => _delegate = Delegate.Combine(_delegate, action) as T;
    public static void Unsubscribe(T action) => _delegate = Delegate.Remove(_delegate, action) as T;
    
}

C#:
Test<OnNoteTriggered>.Subscribe(Show);
Test<OnNoteTriggered>.Unsubscribe(Show);
Test<OnNoteTriggered>.Fire("LOOOOOOOOL");

How does generic static class ? Does it create an instance for each type?
 
How do you like implementation through generic static class?

How optimised and convenient is this option?
I'm not really seeing value of this construction. Perhaps I'm just too deeply ingrained in the C# convention of using EventHander<TEventArgs> that I just see this as a way of a JavaScript style invocation of delegates instead of the C# style invocation of delegates. I prefer writing strongly typed code where errors are caught at compile time. I'm not quite seeing how your Fire() will let me catch errors at compile time.
 
Back
Top Bottom