Protobuf-net to serialize plot model

Oystein Bjorke 10 лет назад 0
This discussion was imported from CodePlex

sharethl wrote at 2014-03-17 15:39:

I am currently exploring how to use protobuf-net to serialize oxyplot.
It should be a nice tool that help on saving and reading plot
But here I got a "Common Language Runtime detected an invalid program." on line "Serializer.Serialize"
Anyone know the issue?
            var PolygonAnnotationType = RuntimeTypeModel.Default.Add(typeof(PolygonAnnotation), false).Add("Points");
            var IDataPointType = RuntimeTypeModel.Default.Add(typeof(IDataPoint), false).Add("X", "Y");
            IDataPointType.AddSubType(100, typeof(DataPoint));

            var poly = new PolygonAnnotation();
            IList<IDataPoint> ps = new List<IDataPoint>() { new DataPoint(0, 0), new DataPoint(1, 0) };
            poly.Points = ps;
            MemoryStream ms = new MemoryStream();
            Serializer.Serialize<PolygonAnnotation>(ms, poly);
            string stringBase64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
            MemoryStream afterStream = new MemoryStream(byteAfter64);
            var newPlot = Serializer.Deserialize<PolygonAnnotation>(afterStream);
            Console.Write(stringBase64);
The following code works. difference is NewDataPoint is a class, instead of structure.
private static void Test1() {

            var poly = new NewPolygon() {
                Points = new List<IDataPoint>(){
                    new NewDataPoint(0,0),new NewDataPoint(1,1)
                }
            };
            RuntimeTypeModel.Default.Add(typeof(NewPolygon), false).Add("Points");
            RuntimeTypeModel.Default.Add(typeof(IDataPoint), false).Add("X","Y").AddSubType(100,typeof(NewDataPoint));

            MemoryStream ms = new MemoryStream();
            Serializer.Serialize<NewPolygon>(ms, poly);
            string stringBase64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
            MemoryStream afterStream = new MemoryStream(byteAfter64);
            var newpoly = Serializer.Deserialize<NewPolygon>(afterStream);
            Console.Write(stringBase64);
        }

class NewDataPoint : IDataPoint {
        public double X { get; set; }
        public double Y { get; set; }
        public NewDataPoint() { }
        public NewDataPoint(double x, double y) {
            this.X = x;
            this.Y = y;
        }
    }
    class NewPolygon : TextualAnnotation {
        public IList<IDataPoint> Points { get; set; }
    }


objo wrote at 2014-03-19 08:31:

This is interesting. How is protobuf handling versioning? The PlotModel and related classes will continue to evolve...

sharethl wrote at 2014-03-19 14:28:

Found the link about versioning. Haven't test yet. Actually I am also have no ideas about this.

Protobuf-net: the unofficial manual

Сервис поддержки клиентов работает на платформе UserEcho