C# template event

I have a "proxy" object that should expose multiple events which could be subscribed to and unsubscribed from. I call this a proxy object since it holds a communication module which requires its users to call Subscribe(topic) and Unsubscribe(topic) to make it start publishing its events.

So I want my users to be able to:

void OnSomthingHappened(What what)
{
    //...
}


EventsGuy.SomethingHappened += OnSomthingHappened;

and in EventsGuy.cs I currently do:

const string SomethingHappenedTopic = "SomethingHappened";
private event Action<What> m_SomethingHappened;
public event Action<What> SomethingHappened
{
    add
    {
        m_SomethingHappened += value;
        if (m_SomethingHappened.GetInvocationList().Length == 1)
        {
            Subscribe(SomethingHappenedTopic, (topic, body) =>
            {
                What what = Deserialize<What>(body);
                m_SomethingHappened?.Invoke(what);
            });
        }
    }
    remove
    {
        if (m_SomethingHappened.GetInvocationList().Length == 1)
        {
            Unsubscribe(SomethingHappenedTopic);
        }
        m_SomethingHappened -= value;
    }
}

The problem is I have a-lot of events to expose and the code is pretty much the same for all of them, with the difference being only:

  • The topic
  • The type of the parameter of the event (in the above example What)

I'm coming from a C++ background and there I would have used templates to solve this with some mapping between the type and the string.

Currently I used a script with that mapping to generate the code for each topic, and I'm interested in a way to use the compiler for this, but I'm not really sure how such a thing could be done with C#. From my understanding Generics can't help much with this, and I can't have a "Base-Delegate" class to use inheritance for this (can I?).

Note that I would like to keep the proxy's interface the same (using C# events) and not convert it to function calls to un/subscribe;