Question

For NetDataContractSerializer msdn states below

"NetDataContractSerializer can be used only if both the serializing and deserializing ends share the same CLR types".

My undertanding is that, If I serialized file in .net4.0 using NetDataContractSerializer and later I shifted all the code to .net4.5 then file with .net 4.0 will not get deserialized eventhough code is same.

Please correct me if my understanding is wrong.

Était-ce utile?

La solution

It depends on the types. NetDataContractSerializer is based on types full type name, possibly qualified with the assembly full name. For example, this code:

List<string> list = new List<string>();
list.Add("joe");
list.Add("sam");

NetDataContractSerializer ser = new NetDataContractSerializer();
using (FileStream stream = File.OpenWrite("test.xml"))
{
    ser.Serialize(stream, list);
}

compiled using CLR 2 will produce this:

<ArrayOfstring z:Id="1" z:Type="System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" z:Assembly="0" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"><_items z:Id="2" z:Size="4"><string z:Id="3">joe</string><string z:Id="4">sam</string><string i:nil="true"/><string i:nil="true"/></_items><_size>2</_size><_version>2</_version></ArrayOfstring>

and the same compiled using CLR 4 will produce this:

<ArrayOfstring z:Id="1" z:Type="System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" z:Assembly="0" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"><_items z:Id="2" z:Size="4"><string z:Id="3">joe</string><string z:Id="4">sam</string><string i:nil="true"/><string i:nil="true"/></_items><_size>2</_size><_version>2</_version></ArrayOfstring>

As you see, there are not compatible because they include System.String's assembly qualified full name. So it will depend on what you do, but chances are good that it will not work as is. I don't have a 4.5 setup at hand, but you could test it directly.

Autres conseils

As DotNet is based on COM technology, i believe dotnet 4.0 and dotnet 4.5 are binary compatible, which means that they have the same CLR types and it will be deserialized.

You might have a look here: MSDN blog on compatibility of Framework 4.5

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top