简体   繁体   English

.NET 紧凑型框架中缺少 DataContractSerializer

[英]DataContractSerializer missing from the .NET Compact Framework

It appears that the DataContractSerializer isn't available in the .NET Compact Framework.似乎 DataContractSerializer 在 .NET 紧凑型框架中不可用。 I found this quite surprising, as I consider DataContractSerializer to be the Holy Grail of serialization, and one of the most widely useful classes introduced in .NET 3.我发现这非常令人惊讶,因为我认为 DataContractSerializer 是序列化的圣杯,并且是 .NET 3 中引入的最广泛使用的类之一。

Is there a way to get the same functionality under the Compact Framework, that is, the ability to easily save and restore real-world object graphs using a textual format?有没有办法在 Compact Framework 下获得相同的功能,即能够使用文本格式轻松保存和恢复真实世界的 object 图形?

I'd rather keep away from the older, limited .NET serializers such as XmlSerializer and BinarySerializer.我宁愿远离旧的、有限的 .NET 序列化器,例如 XmlSerializer 和 BinarySerializer。

I'm thinking perhaps there's a custom implementation of DataContractSerializer available, or maybe it's possible to use the code from Mono?我在想也许有 DataContractSerializer 的自定义实现可用,或者可以使用 Mono 中的代码? Has anyone tried to do this?有没有人尝试过这样做?

Is it not available in CF 3.5?它在 CF 3.5 中不可用吗? (edit: nope, I checked - indeed it is not). (编辑:不,我检查过 - 确实不是)。 I know that XmlSerialzier is there, but yes, it does have some limitations.我知道 XmlSerialzier 在那里,但是是的,它确实有一些限制。

Does it need to be text-based?它需要基于文本吗? On option is protobuf-net , which works on CF2.0, CF3.5, Silverlight, Mono, .NET 2.0, etc;选项是protobuf-net ,它适用于 CF2.0、CF3.5、Silverlight、Mono、.NET 2.0 等; and includes all the things you commonly need (including callbacks etc).并包括您通常需要的所有东西(包括回调等)。

By the way, the Mono code (from "Olive") is, last time I looked, not very complete...顺便说一句,Mono 代码(来自“Olive”)是,上次我看,不是很完整......

(edit) One other option might be Json.NET ; (编辑)另一种选择可能是Json.NET from here , this now works on CF;这里开始,这现在适用于 CF; since this is JSON, it is text-based, and should do most of what you need.因为这是 JSON,它是基于文本的,应该可以满足您的大部分需求。

I don't know if it is fair to call the XmlSerializer "limited."我不知道将 XmlSerializer 称为“有限”是否公平。 What do you mean by that?你是什么意思? Have you used it and found it wanting?你用过它并发现它想要吗? What is the specific thing that fails for you?对你来说失败的具体事情是什么?

In my experience, XmlSerializer actually has richer capability than the DCS.根据我的经验,XmlSerializer 实际上比 DCS 具有更丰富的功能。 The DCS does less, which is probably a good thing. DCS 做的更少,这可能是件好事。 On the other hand the DCS does at least one thing the XmlSerializer does not - and that is it can serialize directly to and from private member vars.另一方面,DCS 至少做了 XmlSerializer 没有做的一件事——那就是它可以直接序列化到私有成员变量和从私有成员变量中序列化。

The XmlSerializer is fast and quite capable, and you can get good utility out of it. XmlSerializer 速度快且功能强大,您可以从中获得良好的实用性。

Found a solution:-)找到了解决方案:-)

There's a great XML serializer at http://www.codeproject.com/KB/XML/GR_CustomXmlSerializer.aspx . http://www.codeproject.com/KB/XML/GR_CustomXmlSerializer.aspx有一个很棒的 XML 序列化器。

As is, the code doesn't work on the Compact Framework, but I've made several trivial adjustments to get it to work, with only a minor loss of functionality.照原样,代码在 Compact Framework 上无法运行,但我已经进行了一些微不足道的调整以使其正常工作,只损失了少量功能。 I've also used OpenNetCF to fill in a few missing CF pieces.我还使用 OpenNetCF 来填充一些缺失的 CF 片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM