序列化排序集成员

时间:2014-08-03 10:06:26

标签: c# serialization

我有一个classe,它派生自有序集并且有一些成员:

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);
        }
    }
}

有了这个枚举:

[Serializable]
public enum CoordinateSystemEnum
{
    Polar,
    Cartesian
}

这个比较者:

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

和这个PolarPosition:

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

我想保存序列化并反序列化它。我使用了这个帖子的答案:Serializing/deserializing with memory stream,代码片段如下:

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);}

现在我的问题是,deserializedTrajectory1.Name总是nulldeserializedTrajectory1.UsedCoordinateSystem总是Polar

我做错了什么?

1 个答案:

答案 0 :(得分:3)

SortedSet<T>类实现ISerializable。根据{{​​3}}文档:

  

当您从实现ISerializable的类派生新类时,派生类必须实现构造函数[采用SerializationInfo和StreamingContext参数]以及GetObjectData方法(如果它具有需要序列化的变量)。

因此,要序列化/反序列化NameUsedCoordinateSystem属性,您需要添加以下逻辑。

[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");
    }
}