Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to store serialization related info between serialization and deserialization
    primarykey
    data
    text
    <p>I'm using serialization with <code>DataContractSerializer</code> and have to save e.g. object which has field of <code>Dictionary&lt;string, IKnownTypesProvider&gt;</code> where <code>IKnownTypesProvider</code> just know which types does it consist from. So I can easily use it to serialize existing object. But if I want to deserialize it later I have to pass a list of known types to <code>DataContractSerializer</code> but I can't do it because I don't know which types were used when that object was serialized. All synthetic samples from MSDN suppress this issue just by serializing and deserializing object in the same method thus having the same set of known types. Can you suggest a way to bypass it? Or maybe you can suggest another mechanism of serialization/deserialization which doesn't have such problem?</p> <p>UPD: @I4V suggested to use Json.NET. However here is my example where it fails. Forget about my initial code. Here is the sample:</p> <pre><code>using System; using System.Collections.Generic; using Newtonsoft.Json; namespace TestConsoleProject { internal class Program { private static void Main(string[] args) { var external = new External(); external.Dictionary.Add(100, new Internal() {InternalCount = 100}); external.Dictionary.Add(200, new DerivedInternal() {InternalCount = 200, DerivedInternalCount = 200}); var settings = new JsonSerializerSettings() {TypeNameHandling = TypeNameHandling.All}; var json = JsonConvert.SerializeObject(external, settings); Console.WriteLine(json); var otherExternal = JsonConvert.DeserializeObject&lt;External&gt;(json); Console.WriteLine(otherExternal.Dictionary[200].GetType()); Console.ReadLine(); } private class External { public Dictionary&lt;int, Internal&gt; Dictionary { get; set; } public External() { Dictionary = new Dictionary&lt;int, Internal&gt;(); } } private class Internal { public int InternalCount { get; set; } } private class DerivedInternal : Internal { public int DerivedInternalCount { get; set; } } } </code></pre> <p>} You can see that the second object in dictionary is serialized as <code>DerivedInternal</code> (which is right) but deserialized as just Internal thus loosing data I need. Can I fix it anyhow?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload