Skip to content

Instantly share code, notes, and snippets.

@Rene-Sackers
Created April 14, 2017 23:00
Show Gist options
  • Save Rene-Sackers/66096a6b278205108ed397ff3b32c7dd to your computer and use it in GitHub Desktop.
Save Rene-Sackers/66096a6b278205108ed397ff3b32c7dd to your computer and use it in GitHub Desktop.
Google Protobuf re-casting
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