簡體   English   中英

序列化排序集成員

[英]serialize sorted set members

我有一個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; }
}

我想保存序列化並反序列化它。 我使用了這個帖子的答案: 使用內存流進行序列化/反序列化 ,代碼片段如下所示:

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始終為null並且deserializedTrajectory1.UsedCoordinateSystem始終為Polar

我究竟做錯了什么?

SortedSet<T>類實現ISerializable 根據自定義序列化文檔:

當您從實現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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM