DataContract 与 Serialized —— 如果您想尽可能高效地序列化(自定义二进制文件),这两者真的很重要吗?
基本上,如果我将我的类标记为 [DataContract]
,我仍然可以编写自己的序列化程序,该序列化程序从 XmlObjectSerializer 扩展,返回二进制文件的方式与我执行 [Serialized]
时的方式相同代码>?
Basically, if I mark my classes as [DataContract]
can I still write my own serializer that extends from XmlObjectSerializer that returns a binary the same way as I could if I did [Serializable]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以,但请记住,如果序列化器继承自
XmlObjectSerializer
,则必须处理 XML。您使用的 XML 编写器/读取器将决定您是否使用二进制表示形式。一种“二进制”XML 读取器/编写器是由XmlDictionaryReader.CreateBinaryReader
和XmlDictionaryWriter.CreateBinaryWriter
创建的。该帖子位于 http://blogs。 msdn.com/b/carlosfigueira/archive/2011/03/29/wcf-extensibility-icontractbehavior.aspx 有一个该示例显示了一个自定义 XmlObjectSerializer,如果与二进制 XML 编写器一起使用,它可以将对象序列化为相当小的大小。
Yes you can, but remember that the serializer, if inheriting from
XmlObjectSerializer
will have to deal with XML. Which XML writer / reader you use will determine whether you're using a binary representation or not. One "binary" XML reader/writer is the one created byXmlDictionaryReader.CreateBinaryReader
andXmlDictionaryWriter.CreateBinaryWriter
.The post at http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/29/wcf-extensibility-icontractbehavior.aspx has a sample which shows a custom XmlObjectSerializer which, if used with a binary XML writer, can serialize objects to a fairly small size.