3

I have a classe which derives from sorted set and has some members:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Application.Model
{
    [Serializable]
    public class Trajectory : SortedSet<PolarPosition>
    {
        public delegate void DataChangedEventHandler();
        public event DataChangedEventHandler DataChanged = delegate { };

        public Trajectory()
            : base(new PolarPositionComparer())
        {
            this.Name = "new one";
        }

        public Trajectory(IComparer<PolarPosition> comparer)
            : base(comparer)
        {
            this.Name = "new compared one";
        }

        public Trajectory(IEnumerable<PolarPosition> listOfPoints)
            : base(listOfPoints, new PolarPositionComparer())
        {
            this.Name = "new listed one";
        }

        public Trajectory(SerializationInfo info, StreamingContext context)
            : base(info, context)
        { }

        public string Name { get; set; }

        public CoordinateSystemEnum UsedCoordinateSystem { get; set; }

        public new bool Add(PolarPosition point)
        {
            DataChanged();
            return base.Add(point);
        }
    }
}

With this enum:

[Serializable]
public enum CoordinateSystemEnum
{
    Polar,
    Cartesian
}

this comparer:

[Serializable]
public class PolarPositionComparer : IComparer<PolarPosition>
{
    ...
}

and this PolarPosition:

[Serializable]
public class PolarPosition
{
    public double Radius { get; set; }
    public double Phi { get; set; }
}

I want to save serialize and deserialize it. I used the answer from this thread: Serializing/deserializing with memory stream and the snippet looks like:

var trajectory1 = new Trajectory { 
                    new PolarPosition(10, 2), 
                    new PolarPosition(11, 2), 
                    new PolarPosition(12, 2) 
                  };

trajectory1.Name = "does ont matter";
trajectory1.UsedCoordinateSystem = CoordinateSystemEnum.Cartesian;
var memoryStreamOfTrajectory1 = SerializeToStream(trajectory1);
var deserializedTrajectory1 = DeserializeFromStream(memoryStreamOfTrajectory1);}

Now my Problem is, that deserializedTrajectory1.Name is always null and deserializedTrajectory1.UsedCoordinateSystem is always Polar.

What am I doing wrong?

Community
  • 1
  • 1
Rico-E
  • 1,806
  • 2
  • 28
  • 47
  • 1
    Well, how are you serializing the data? Architecturally, you should probably not derive from SortedSet but wrap it. – usr Aug 03 '14 at 10:11

1 Answers1

3

The SortedSet<T> class implements ISerializable. Per the Custom Serialization documentation:

When you derive a new class from one that implements ISerializable, the derived class must implement both the constructor [that takes SerializationInfo and StreamingContext parameters] as well as the GetObjectData method if it has variables that need to be serialized.

Thus, to serialize/deserialize your Name and UsedCoordinateSystem properties, you would need to add the following logic.

[Serializable]
public class Trajectory : SortedSet<PolarPosition>
{
    protected override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue("Name", this.Name);
        info.AddValue("UsedCoordinateSystem", (int)this.UsedCoordinateSystem);
    }

    public Trajectory(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        this.Name = info.GetString("Name");
        this.UsedCoordinateSystem = (CoordinateSystemEnum)info.GetInt32("UsedCoordinateSystem");
    }
}
Douglas
  • 53,759
  • 13
  • 140
  • 188