Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryFormatter and Deserialization Complex objects

Tags:

Can not deserialize following object graph. That Exception occurs when deserialize method called on BinaryFormmater: System.Runtime.Serialization.SerializationException :

The constructor to deserialize an object of type 'C' was not found. 

There're two constructor on C. and I think the problem may be : While serialization Binaryformatter using the paramatered one and on deserialization process, it needs a parameterless one. Is there a hack / solution? Objects :

  [Serializable]     public class A     {         B b;         C c;          public int ID { get; set; }          public A()         {         }          public A(B b)         {             this.b = b;         }          public A(C c)         {             this.c = c;         }     }     [Serializable]     public class B     {      }     [Serializable]     public class C : Dictionary<int, A>     {         public C()         {          }          public C(List<A> list)         {             list.ForEach(p => this.Add(p.ID, p));         }     } 

// Serialization success

    byte[] result;     using (var stream =new MemoryStream())     {         new BinaryFormatter ().Serialize (stream, source);         stream.Flush ();         result = stream.ToArray ();     }     return result; 

// Deserialization fails

    object result = null;     using (var stream = new MemoryStream(buffer))     {         result = new BinaryFormatter ().Deserialize (stream);     }     return result; 

The calls are at the same environment, same thread, same method

        List<A> alist = new List<A>()         {             new A {ID = 1},             new A {ID = 2}         };          C c = new C(alist);         var fetched = Serialize (c); // success         var obj = Deserialize(fetched); // failes 
like image 272
jack-london Avatar asked Feb 16 '11 13:02

jack-london


People also ask

What is meant by serialization & deserialization?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is deserialization of object?

Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.

Why do we need serialization and deserialization?

Serialization is the process of converting an object into a stream so that it can be saved in any physical file like (XML) or can be saved in Database. The main purpose of Serialization in C# is to persist an object and save it in any specified storage medium like stream, physical file or DataBase.

Does deserialization create new object?

When you deserialize your object, the object will create a new entry in heap which will not have any references to any of the objects.


1 Answers

I suspect you just need to provide a deserialization constructor to C, since dictionary implements ISerializable:

protected C(SerializationInfo info, StreamingContext ctx) : base(info, ctx) {} 

checked (passes):

 static void Main() {      C c = new C();      c.Add(123, new A { ID = 456});      using(var ms = new MemoryStream()) {          var ser = new BinaryFormatter();          ser.Serialize(ms, c);          ms.Position = 0;          C clone = (C)ser.Deserialize(ms);          Console.WriteLine(clone.Count); // writes 1          Console.WriteLine(clone[123].ID); // writes 456      }  } 
like image 119
Marc Gravell Avatar answered Sep 30 '22 03:09

Marc Gravell