I am trying to create a simple in-memory grid that can be serialized using protocol buffers.
The idea is the user can create/define columns of any type (primitives or user defined as long as they're protocol buffers tagged).
My problem is you can't serialize Type data with protocol buffers so how can i achieve this?
Code is below showing how i'd hoped to code the Columns of the grid.
Thanks a lot.
[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();
}
}
}