NetDataContractSerializer and CLR types

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.

Answers


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.


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


Need Your Help

Two controllers, one scope variable in Angular

javascript angularjs

Okay so I have two controllers that control two distinctly different parts of the site, that are on the same page.

Shoutcast aac+ player app android like tunein

android flash builder

Im looking for an example or a guide to how create an app like tunein to have my own list of stations then if i make click in one shows me a player to play de stream i basically use aac+ also have ...