Silverlight 4 和 System.Runtime.Serialization
我有一个包含一些业务对象的 Silverlight 4 项目。我添加了一个测试项目。其中一项测试是使用 DataContractSerializer
序列化 Silverlight 项目中的业务对象。要引用 DataContractAttribute
,我必须添加对 System.Runtime.Serialization
的引用。但是,Silverlight 运行时和测试项目的 .NET 4 运行时中存在不同且明显不兼容的版本。
在 Silverlight 项目中序列化对象以便标准 .NET 4 运行时可以使用它们的最佳策略是什么?
I have a Silverlight 4 project that contains some business objects. I added a Test project. One of the tests is to serialize the business objects from the Silverlight project using DataContractSerializer
. To reference DataContractAttribute
, I have to add a reference to System.Runtime.Serialization
. However, there are different and apparently incompatible versions in the Silverlight runtime and in the .NET 4 Runtime of the Test project.
What's the best strategy to serialize objects in a Silverlight project such that they can be consumed by a standard .NET 4 runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,我将使用 DataContractJsonSerializer。这会将您的业务对象序列化为 json。自 3.5 起,Silverlight 和 .NET Framework 就支持它。
可以在此处找到如何使用序列化程序的示例。
For this purpose i would use the DataContractJsonSerializer. This serializes your business objects to json. It it supported in Silverlight and .NET Framework since 3.5.
An example how to use the serializer can be found here.
如果您的对象允许(关于下面提到的约束),请尝试序列化器,例如 protobuf-bet 或其他协议缓冲区实现之一。
http://code.google.com/p/protobuf-net/
他们放置对业务对象(更确切地说是合同)的实现有一些限制,例如 null 和空集合被认为等同于举一个例子。
If your objects allow it (regarding constraints mentioned below), try a serializer such as protobuf-bet or one of the other protocol buffer implementations.
http://code.google.com/p/protobuf-net/
They place some constraints on the implementation of your business objects (contracts, rather), for example that null and empty collections are considered equivalent to name one example.
今天早上我意识到我已经从非 Silverlight 项目添加了对 Silverlight 项目的引用,并且 VS 警告这不是一个好主意,但仍然允许非 Silverlight 项目运行。由于每个项目中都引用了不同版本的 System.Runtime.Serialization,因此单元测试因无法加载该 DLL 而中止。
I realized this morning that I had added a reference to a Silverlight project from a non-Silverlight project and VS warned about that not being a good idea, but still allowed the non-Silverlight project to run. Because a different version of
System.Runtime.Serialization
was referenced in each project, the unit test aborted with an inability to load that DLL.