Is there any way at all to achieve the use of downstream delegate definitions in an abstract parent class?
Delegates, like types, can't be overridden:
public abstract class ActionIssuerSimple
{
public delegate void ActionHandler();
...
public void Subscribe(ActionHandler handler) { ... }
}
public abstract class ActionIssuer<TAction> : ActionIssuerSimple
{
public override delegate void ActionHandler(TAction parameter);
...
}
The above gives the error "delegate cannot be override"
The goal is, given an example of SillyIssuer : ActionIssuer<SillyAction>
, for me to be able to use sillyIssuer.Subscribe((SillyAction action) => { ... });
(in addition to using ActionIssuerSimple for actions that can run handlers with the signature () => { }
. Because C# doesn't support using downstream Delegate definitions in an abstract class, neither through abstract
, generic type parameters, nor override
, I can't quite find a way to do this without either:
- Copying all of the subscription functionality from ActionIssuerSimple into ActionIssuer. This is undesirable because it isn't DRY. Most of the code in ActionIssuerSimple revolves around manipulating a list of ActionHandlers.
- Having all ActionIssuers that don't need to pass an Action object pass one anyways, something like an int to reduce cost. This isn't desirable because it uses up unnecessary memory bandwidth in a high-traffic core loop for a performance-critical application. It also leaves me with a swath of unused parameter warnings in the ActionHandler methods.