Skip to content

Instantly share code, notes, and snippets.

@jonpryor
Created November 20, 2012 02:49
Show Gist options
  • Save jonpryor/4115617 to your computer and use it in GitHub Desktop.
Save jonpryor/4115617 to your computer and use it in GitHub Desktop.
// from: http://pastebin.com/Hg8vkNAu
//
// Suggested improvement:
static readonly Dictionary<Type, Func<byte[], object>> Decoders = new Dictionary<Type, Func<byte[], object>>() {
{ typeof (string), x => Encoding.UTF8.GetString(x) },
{ typeof (byte[]), x => x },
};
public void Add<T>(string cmd, MessageReceiver<T> receiver)
{
Func<byte[], object> decoder;
if (!Decoders.TryGetValue (typeof (T), out decoder))
decoder = x => JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(x));
Receivers.Add (cmd, (data, headers) => {
T message = (T) decode(data);
receiver(message, headers);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment