Thursday 15 July 2010

c# - Dictionary not deserializing -


I have a problem where a dictionary in my project is either not serializing or deserializing after Deserializing, the data I serialized The bus is not in the object.

Here the relevant cutting of the class is serialized:

  class person: ISerializable {personal dictionary & lt; Relation, from the list & lt; Integer & gt; & Gt; Relationships = New Dictionary & lt; Relationship, list & lt; Int & gt; & Gt; (); ("Relationship", typef (dictionary & lt; relationship, list & lt; int & gt; & gt;);). } Public Zero GetObjectData (SerializationInfo Information, Streaming Conference Reference) {info.AddValue ("Relationships", This Relationships); }}  

Note, this is binary serialization, everything in project series and deserialzes properly.

This works fine for me:

  System usage Tax; Using System.Collections.Generic; Use the system. time. Timeliness; Using System.Runtime.Serialization.Formatters.Binary; Using System.IO; Class program {[serializable] class relations {} [serializable] class person {personal dictionary & lt; Relationship, list & lt; Int & gt; & Gt; Relationships = New Dictionary & lt; Relationship, list & lt; Int & gt; & Gt; (); Public person () {} Public Zero AddRelationship () {Relationships. (New connection (), new list & lt; int & gt; ()); } Public int count () {Return relationships; Calculation; } / * Public person (serializationInfo information, streaming contact reference) {this.Relationships = (dictionary & lt; int, list & lt; int & gt; & gt;) info.GetValue ("relationship", typef (dictionary) ; Relationship, list & lt; int & gt; & gt;);; } Public Zero GetObjectData (SerializationInfo Information, Streaming Conference Reference) {info.AddValue ("Relationships", This Relationships); } * * /} Public static zero main () {person person = new person (); Person.AddRelationship (); BinaryFormatter formatter = new BinaryFormatter (); Memorystream stream = new memorystream (); Formatter.Serialize (stream, person); Stream Search (0, SeekOrigin.Begin); Person = (person) formatter.Deserialize (section); Console.light line ("count:" + person. Counts ()); }}  

Note: The other manufacturer and GetObjectData are not required, although adding them can not break anything if I'm making your code bigger, then maybe you do some work You can post a modified version of the Runnabal code that shows the failure you saw. You can either remove things from your code unless it is simple enough to post a compiling version without the need for external sections, or do not reproduce this error until you add things to - whatever your Easy to do

that regenerates the error in the process of creating a simple movable version, so that you can solve the problem.


No comments:

Post a Comment