问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/7266198/silverlight-4-and-system-runtime-serialization