I am trying to create a simple grid in memory that can be serialized using protocol buffers.
The idea is that the user can create / define columns of any type (primitives or user-defined definitions, if they are protocol buffers).
My problem is that you cannot serialize type data with protocol buffers, so how can I achieve this?
The code below shows how I was hoping to encode the grid columns.
Many thanks.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Column colOrderId = new Column("OrderId", typeof(uint));
Column colOrderDesc = new Column("OrderDesc", typeof(string));
Column colPrice = new Column("Price", typeof(Price));
Column colOrderDateTime = new Column("OrderDateTime", typeof(DateTime));
var s = colOrderId.ToBArray();
}
}
[ProtoContract, Serializable]
public sealed class Column
{
public Column(string name, Type type)
{
Name = name;
Type = type;
}
[ProtoMember(1)]
public string Name { get; private set; }
[ProtoMember(2)]
public Type Type { get; private set; }
}
[ProtoContract, Serializable]
public sealed class Price
{
public Price(double value, string currency)
{
Value = value;
Currency = currency;
}
[ProtoMember(1)]
public double Value { get; private set; }
[ProtoMember(2)]
public string Currency { get; private set; }
}
public static class ProtoBufEx
{
public static byte[] ToBArray<T>(this T o)
{
using (MemoryStream ms = new MemoryStream())
{
ProtoBuf.Serializer.Serialize(ms, o);
return ms.ToArray();
}
}
}
source
share