Skip to content

Instantly share code, notes, and snippets.

@doctorpangloss
Last active December 14, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctorpangloss/5120220 to your computer and use it in GitHub Desktop.
Save doctorpangloss/5120220 to your computer and use it in GitHub Desktop.
// Provide an argument to a "multiplexing" event
class EventDispatcher {
public delegate void EventHandler(MessageEvent e);
public event EventHandler MultiplexedEvent;
}
class MessageEvent {
int MessageType; // Guess what component I'm working on which does something very similar to this
object Data;
}
EventDispatcher dispatcher = new EventDispatcher();
// Current semantics
dispatched.MultiplexedEvent +=
(e) => Diagnostics.Trace.WriteLine(e.MessageType == 1 ? "Message type 1 handled." : "Message type 1 not received".);
dispatched.MultiplexedEvent +=
(e) => Diagnostics.Trace.WriteLine(e.MessageType == 2 ? "Message type 2 handled." : "Message type 2 not received".);
// Desired semantics
dispatched.MultiplexedEvent(1) += (e) => Diagnostics.Trace.WriteLine("Message type 1 handled.");
dispatched.MultiplexedEvent(2) += (e) => Diagnostics.Trace.WriteLine("Message type 1 handled.");
// Of course it would be most awesome to do this, but I recognize it's really only possible in God's language, JavaScript
dispatched.MessageType1 += (e) => ...
dispatched.MessageType2 += (e) => ...
@mthomas
Copy link

mthomas commented Mar 8, 2013

Actually you can do that in c# using dynamic types, probably....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment