❔ Help with Action syntax for event handler system

I am trying to add a basic event system to my program and would like the syntax to be something like this:
RegisterEventHandler<EventHandler, MyEvent>(x.Process);
RegisterEventHandler<EventHandler, MyEvent>(x.Process);
Ideally there is no need for a baseclass or interface. All that should be needed is for the function passed as an action to take the event type as an argument. I have the following implmentation for the registration:
void RegisterEventHandler<THandler, TEvent>(Action<THandler, TEvent> action)
void RegisterEventHandler<THandler, TEvent>(Action<THandler, TEvent> action)
I would very much like the register syntax to be like this:
RegisterEventHandler<EventHandler, MyEvent>(x.Process);
RegisterEventHandler<EventHandler, MyEvent>(x.Process);
but I struggle with how to achive this, and I am not entirely sure if its possible?
5 Replies
Angius
Angius2y ago
x => x.Process would be the syntax for an action And, no, it cannot be shortened to x.Process
Ole (ping please)
I see I messed up my post a bit: This is working:
RegisterEventHandler<EventHandler, MyEvent>((x, e) => x.Process(e));
RegisterEventHandler<EventHandler, MyEvent>((x, e) => x.Process(e));
But the (x,e) syntax is not all that clean. So I would love doing x.Process and have the e implied
Angius
Angius2y ago
I don't think you can do that
Ole (ping please)
I feared so, thanks for clarifying. I dont deal with generics all that much and the rules for when things can be implied or not is still a bit of a mystery to me 🙂
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server