如何在C#反序列化期间进行引用修复?

时间:2016-06-21 12:31:45

标签: c# serialization deserialization

如何使用C#序列化框架引用修正(后处理)?

我有一个对象图,其中的对象引用其他对象。它们都实现了ISerializable接口,并且它们都具有实例ID,因此表示序列化状态中的引用很容易。

krux是当调用​​反序列化构造函数时,该对象引用的所有对象可能都没有被反序列化,因此引用不能设置为有效对象。我找不到任何方法可以挂钩C#序列化框架中的后处理步骤来进行参考修复。有办法吗?

根据要求,这是一个人为的课程,我认为这个课程突出了问题。

[Serializable]
public class Pony : ISerializable
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Pony BFF { get; set; }

  public Pony() {}
  private Pony(SerializationInfo info, StreamingContext context) {
    Id = info.GetInt32("id");
    Name = info.GetString("name");
    var bffId = info.GetInt32("BFFId");
    BFF = null; // <===== Fixup! Find Pony instance with id == bffId
  }

  public void GetObjectData(SerializationInfo info, StreamingContext ontext) {
    info.AddValue("id", Id);
    info.AddValue("name", Name);
    info.AddValue("BFFId", BFF.Id);
  }
}

这是(de)序列化代码:

var rd = new Pony { Id = 1, Name = "Rainbow Dash" };
var fs = new Pony { Id = 2, Name = "Fluttershy", BFF = rd };
rd.BFF = fs;
var ponies = new List<Pony>{ rd, fs };
Stream stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, ponies);
stream.Seek(0, SeekOrigin.Begin);
var deserializedPonies = (List<Pony>)formatter.Deserialize(stream);

这个问题无法解决我的问题:.net XML Serialization - Storing Reference instead of Object Copy

我想使用BinaryFormatter + ISerializable框架进行序列化,而不是切换到XmlFormater。

1 个答案:

答案 0 :(得分:1)

为此目的有一个属​​性。

在要反序列化的任何对象中实现以下方法:

[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context) {

}

System.Runtime.Serialization中还有一些属性可能会对您有所帮助。

修改

我稍微修改了你的代码:

[Serializable]
  public class Pony  {
    public int Id {
      get; set;
    }
    public string Name {
      get; set;
    }
    public Pony BFF {
      get; set;
    }

    public Pony() {
    }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context) {
      Console.WriteLine(this.Id + " " + this.Name + " " + this.BFF?.Name);
    }
  }

TestMethod的:

var rd = new Pony { Id = 1, Name = "Rainbow Dash" };
      var fs = new Pony { Id = 2, Name = "Fluttershy", BFF = rd };
      rd.BFF = fs;
      var ponies = new List<Pony> { rd, fs };

      object returnValue;
      using (var memoryStream = new MemoryStream()) {
        var binaryFormatter = new BinaryFormatter();
        binaryFormatter.Serialize(memoryStream, ponies);
        memoryStream.Position = 0;
        returnValue = binaryFormatter.Deserialize(memoryStream);
      }
      var xx = (List<Pony>)returnValue;

如您所见,我删除了ISerializable界面,私有构造函数和GetObjectData - 方法。

我这样做了,因为我不认为你真的需要它,因为你没有说过,你已经实现了(De)序列化。

This帖子是另一个信息来源

编辑2 (Testmethod保持不变):

方法一(完全反序列化和序列化) ...

    private Pony(SerializationInfo info, StreamingContext context) {

      foreach (SerializationEntry entry in info) {
        switch (entry.Name) {
          case "Id":
            this.Id = (int)entry.Value;
            break;
          case "Name":
            this.Name = (string)entry.Value;
            break;
          case "BFF":
            this.BFF = (Pony)entry.Value;
            break;
        }
      }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ontext) {
      info.AddValue("Id", Id);
      info.AddValue("Name", Name);
      info.AddValue("BFF", BFF);
    }
  }

...

方法2(递归对象 - 仅限Id):

...

private Pony(SerializationInfo info, StreamingContext context) {

      foreach (SerializationEntry entry in info) {
        switch (entry.Name) {
          case "Id":
            this.Id = (int)entry.Value;
            break;
          case "Name":
            this.Name = (string)entry.Value;
            break;
          case "BFF.Id":
            var bffId = (int)entry.Value;
            this.BFF = GetPonyById(bffId); // You have to implement this
            break;
        }
      }
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ontext) {
      info.AddValue("Id", Id);
      info.AddValue("Name", Name);
      info.AddValue("BFF.Id", BFF.Id);
    }

...