gpt4 book ai didi

c# - 实现 ISerializable 时永远不会命中 GetObjectData() 方法

转载 作者:可可西里 更新时间:2023-11-01 08:39:48 25 4
gpt4 key购买 nike

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。 Binary 和 soap 都可以。如果要管理 xml 序列化,请使用 IXmlSerializable相反

关于c# - 实现 ISerializable 时永远不会命中 GetObjectData() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9950068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com