CMertens / protobuf-net

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

Member of derived type is not deserialized correctly if in constructor is instantiated to a different type having the same base #225

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Following code:

  static void Main(string[] args)
        {

            MyFigure myFigure = new MyFigure();

            MyGeometryEllipse ellipse = new MyGeometryEllipse();
            myFigure.MyGeometry = ellipse;

            MemoryStream memoryStream = new MemoryStream();
            ProtoBuf.Serializer.Serialize<MyFigure>(memoryStream, myFigure);
            memoryStream.Position = 0;
            object deserialized = ProtoBuf.Serializer.Deserialize<MyFigure>(memoryStream);

       }
    }

    [ProtoContract]
    [ProtoInclude(1, typeof(MyFigure))]
    abstract class MyDrawing
    { }

    [ProtoContract]
    class MyFigure : MyDrawing
    {
        [ProtoMember(1)]

        internal MyGeometry MyGeometry { get; set; }

        internal MyFigure()
        {
            //If I do not comment the following line deserialized
            // object will have a geometry of type point regardless
            // of my assignment to ellipse
            MyGeometry = new MyGeometryPoint();
        }

    }

    [ProtoContract]
    [ProtoInclude(1, typeof(MyGeometryEllipse))]
    [ProtoInclude(2, typeof(MyGeometryPoint))]
    abstract class MyGeometry
    {}

    [ProtoContract]
    class MyGeometryEllipse : MyGeometry
    {}

    [ProtoContract]
    class MyGeometryPoint : MyGeometry
    {}

What is the expected output? What do you see instead?

I would expect the deserialized object to have MyGeometry of type 
MyGeometryEllipse

What version of the product are you using? On what operating system?

rev 444

Please provide any additional information below.

Can this be handled via attributes?

alexandru.iordan@gmail.com

Original issue reported on code.google.com by alexandr...@gmail.com on 2 Sep 2011 at 9:07