Note that there are some explanatory texts on larger screens.

plurals
  1. POprotobuf-net class with dictionary property
    primarykey
    data
    text
    <p>Given the following classes,</p> <pre><code>public static void Test() { var bigDictionary = new Dictionary&lt;string, TestClass&gt;(); bigDictionary.Add("entry1", new TestClass()); bigDictionary.Add("entry2", new TestClass()); bigDictionary.Add("entry3", new TestClass()); // EDIT: Added these lines bigDictionary["entry1"].MyDictionary.Add(1, new SecondTestClass() { MyVar = "hello" }); bigDictionary["entry1"].MyDictionary.Add(2, new SecondTestClass() { MyVar = "world" }); bigDictionary["entry2"].MyDictionary.Add(1, new SecondTestClass() { MyVar = "please" }); bigDictionary["entry2"].MyDictionary.Add(2, new SecondTestClass() { MyVar = "work" }); using (var file = System.IO.File.Create("test.temp")) { ProtoBuf.Serializer.Serialize(file, bigDictionary); } Dictionary&lt;string, TestClass&gt; outDict = null; using (var file = System.IO.File.OpenRead("test.temp")) { outDict = ProtoBuf.Serializer.Deserialize&lt;Dictionary&lt;string, TestClass&gt;&gt;(file); } // EDIT: You'll notice these asserts fail System.Diagnostics.Debug.Assert(outDict["entry1"].MyDictionary.ContainsKey(1)); System.Diagnostics.Debug.Assert(outDict["entry1"].MyDictionary.ContainsKey(2)); System.Diagnostics.Debug.Assert(outDict["entry2"].MyDictionary.ContainsKey(1)); System.Diagnostics.Debug.Assert(outDict["entry2"].MyDictionary.ContainsKey(2)); } [System.Runtime.Serialization.DataContract] public class TestClass { public TestClass() { MyDictionary = new Dictionary&lt;int,SecondTestClass&gt;(); } [System.Runtime.Serialization.DataMember] public Dictionary&lt;int, SecondTestClass&gt; MyDictionary { get; set; } } [System.Runtime.Serialization.DataContract] public class SecondTestClass { [System.Runtime.Serialization.DataMember] public string MyVar { get; set; } } </code></pre> <p>(How) can I get <code>bigDictionary</code> to serialize without throwing the following exception?</p> <pre><code>"Only data-contract classes (and lists/arrays of such) can be processed (error processing Dictionary`2)" </code></pre> <p>(I have tried tagging with DataContract/DataMember ProtoContract/ProtoMember etc. but nothing seems to work)</p> <p>Thanks in advance for any help.</p> <p>EDIT: Seems I have kinda worked it out. Given the above edited code, you'll notice the asserts fail. So it would seem that the inner dictionary is not being serialized at all. However, if I change the DataContract tags to Protobuf.ProtoContract (and DataMember to ProtoMember), it correctly picks up the inner dictionary. </p> <p>Am I mis-using the library or is this a bug?</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.
 

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