实现ISerializable时

实现ISerializable时

本文介绍了实现ISerializable时,永远不会使用GetObjectData()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XmlSerializer 从不在我的 ISerializable 上调用 GetObjcetData().什么时候调用 GetObjectData()?谢谢!

XmlSerializer never calls GetObjcetData() on my ISerializable. When is GetObjectData() called? Thanks!

class Program
{
  static void Main(string[] args)
  {
    var thing = new Thing { Name = "Dude", Id = 1 };
    var xmlSerializer = new XmlSerializer(typeof(Thing));

    var sw = new StringWriter();
    xmlSerializer.Serialize(sw, foo);
    var serializedXml = sw.ToString();

    var sr = new StringReader(serializedXml);
    var result = (Thing)xmlSerializer.Deserialize(sr);
  }
}

public class Thing : ISerializable
{
  public string Name { get; set; }
  public int Id { get; set; }

  public Thing() { }
  public Thing(SerializationInfo info, StreamingContext context) { }

  public void GetObjectData(SerializationInfo info, StreamingContext context)
  {
    // Breakpoint placed on the following line never gets hit:
    throw new NotImplementedException();
  }
}

推荐答案

XmlSerializer不调用GetObjectData.二进制和肥皂.如果要管理xml序列化,请使用 IXmlSerializable 代替

XmlSerializer doesn't call GetObjectData. Binary and soap do.If you want to manage xml serialization, use IXmlSerializable instead

这篇关于实现ISerializable时,永远不会使用GetObjectData()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:13