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

标签 c# serialization deserialization

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

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

关键在于,当调用反序列化构造函数时,该对象引用的所有对象可能都没有被反序列化,因此不能将引用设置为有效对象。而且我找不到任何方法来连接到 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);
  }
}

这是(反)序列化代码:

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。

最佳答案

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

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

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

测试方法:

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 接口(interface)、私有(private)构造函数和您的 GetObjectData - 方法。

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

This帖子是另一种信息来源

EDIT 2(测试方法保持不变):

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

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

...

关于c# - 如何在 C# 反序列化期间进行引用修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37944527/

相关文章:

c# - 在出现 OnlyOnFaulted 的情况下使用 TPL 时出现未处理的异常

c# - 以下在c#中使用expression-bodied语法的方式有区别吗?

json - 使用 Serde 将两种类型转换为一种类型

java - 序列化 hibernate 模型及其延迟加载对象

c# - c# 中的 protected 与 protected 内部(再次)

c++ - Boost:重新使用/清除 text_iarchive 以反序列化来自 Asio:receive() 的数据

laravel - 如何在laravel数据表中添加行号或序列号

c++ - 使用 StrTk String Toolkit Library 编写 CSV 的示例

c# - json.net 反序列化未知类型,直到反序列化其父级?

c# - Linq 相当于一个数据库行程中多个表的聚合函数