Created
April 14, 2017 23:00
-
-
Save Rene-Sackers/66096a6b278205108ed397ff3b32c7dd to your computer and use it in GitHub Desktop.
Google Protobuf re-casting
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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using ProtoBuf; | |
namespace TestConsoleApp | |
{ | |
[ProtoContract] | |
public class TestClass | |
{ | |
[ProtoMember(1)] | |
public string Value { get; set; } | |
} | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var testClass = new TestClass { Value = "test" }; | |
var handlerList = new Dictionary<Type, dynamic>(); | |
handlerList.Add(typeof(TestClass), new Action<TestClass>(TCHandler)); | |
byte[] serializedBytes; | |
using (var ms = new MemoryStream()) | |
{ | |
Serializer.Serialize(ms, testClass); | |
serializedBytes = ms.ToArray(); | |
} | |
dynamic deserializedClass; | |
using (var ms = new MemoryStream(serializedBytes)) | |
{ | |
deserializedClass = Serializer.Deserialize(typeof(TestClass), ms); | |
} | |
deserializedClass = Convert.ChangeType(deserializedClass, typeof(TestClass)); | |
handlerList[typeof(TestClass)].Invoke(deserializedClass); | |
Console.ReadKey(); | |
} | |
private static void TCHandler(TestClass testClass) | |
{ | |
Console.WriteLine(testClass.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment