andyQn / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Cannot serialize structure. #435

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1. copy this code
2. paste to new console program
3. run

What is the expected output? What do you see instead?
Expected to see serialized string,
but saw "Common Language Runtime detected an invalid program."
on Serializer.Serialize<>()

What version of the product are you using? On what operating system?
2.0.0.668, Win7 64bit

Please provide any additional information below.

private static void Test2() {
            var poly = new PolygonAnnotation() {
                Points = new List<IDataPoint>(){
                    new NewDataPoint(0,0),new NewDataPoint(1,1)
                }
            };
            RuntimeTypeModel.Default.Add(typeof(PolygonAnnotation), false).Add("Points");
            RuntimeTypeModel.Default.Add(typeof(IDataPoint), false).Add("X", "Y").AddSubType(100, typeof(NewDataPoint));
            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 newpoly = Serializer.Deserialize<PolygonAnnotation>(afterStream);
            Console.Write(stringBase64);
        }

public interface IDataPoint{
        double X { get; set; }
        double Y { get; set; }
    }
struct NewDataPoint : IDataPoint {
        private double x;
        private double y;

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

Original issue reported on code.google.com by tonghua...@gmail.com on 17 Mar 2014 at 3:34

GoogleCodeExporter commented 8 years ago
Please reference to oxyplot.

Original comment by tonghua...@gmail.com on 17 Mar 2014 at 3:35