c# – GetObjectData()方法在实现ISerializable时永远不会被打

前端之家收集整理的这篇文章主要介绍了c# – GetObjectData()方法在实现ISerializable时永远不会被打前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
XmlSerializer从不在我的ISerializable上调用GetObjcetData().什么时候调用GetObjectData()?谢谢!
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

猜你在找的C#相关文章