Automatic creation of custom events at runtime
I have a class that handles sending and receiving over a socket between my application and the network. This class uses other classes, including the low-level sockket connection class and the PDU handler class, which creates messages to send and process received data.
I now use an event to signal to my class that the low level connection class has data for it, and I need to send that data to a PDU handler to convert information that the application can use and then pass the data to the application.
For future use, I'm trying to get the class as generic as possible, so that on future Server / Client projects I will only need to change the PDU handler to take into account the new operations available and how to handle the data.
Everything is currently happening and now I am facing the problem of transferring data back to the application. For this my logical approach is an event to let the application know that the data is ready to be collected. For this I can either:
a) have one event and allow the application to parse which message it is via opcode (doable) b) Have one event for each opcode and subscribe to all applications and thus know where to start
With the idea of creating generic things and the approach outlined in b, is there a way to dynamically create events based on a given delegate signature at runtime?
That is, imagine you have opcodes in an enumeration called MyOperation1 and MyOperation2, and you have defined a delegate, for example:
public delegate void PDUEventHandler(ParamType Param, [...]);
and I want to define the triggered events:
public event PDUEventHandler MyOperation1; public event PDUEventHandler MyOperation2;
But if I add a new opcode, I need an event for it.
Can these events be generated dynamically or do I need to do it manually? If I need to do it manually, I think one event would be better and handle the application side.
source to share
Perhaps you need a callback - essentially, you pass a delegate to the event handler to execute when the handler is done. fooobar.com/questions/60158 / ...
In terms of event handlers and reusability, perhaps you can extend EventArgs and provide this delegate as a property.
I was thinking of the only one
that has a common code and a "hole" where custom code runs. This custom code is passed to the handler as a delegate (i.e. a Method) or even a class instance. But let it change a little ...
It looks like you need a factory . In fact, you are practically describing a factory.
Conceptually to let go of the idea of transmitting special opcodes
as such, or with multiple
s signatures .
Create a class
. factory returns a custom instance as a generic class reference
. Then, instead of PDUEventHander, you have a link
that points to a factory -custom instance.
source to share