Created
November 20, 2012 02:49
-
-
Save jonpryor/4115617 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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