BinaryFormatter and Deserialization Complex objects

佐手、 提交于 2019-12-17 11:28:51

问题


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

回答1:


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
     }
 }



回答2:


Your serialization will succeed when you implement class C as following:

[Serializable]
public class C : IDictionary<int,A>
{
    private Dictionary<int,A> _inner = new Dictionary<int,A>;

    // implement interface ...
}

The problem is the serialization of the Dictionary derived class.



来源:https://stackoverflow.com/questions/5017274/binaryformatter-and-deserialization-complex-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!