BinaryFormatter und Deserialisierung Komplexer Objekte

Kann nicht deserialisiert werden folgende Objektdiagramm. Diese Ausnahme tritt auf, wenn Deserialisieren Methode aufgerufen BinaryFormmater:
System.- Laufzeit.Serialisierung.SerializationException :

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

Es sind zwei Konstruktor auf C. und ich denke, das problem kann sein : Während der Serialisierung Binaryformatter mit der paramatered und Deserialisierung, es muss einen parameterlosen. Gibt es ein hack /Lösung?
Objekte :

  [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));
        }
    }

//Serialisierung Erfolg

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

//Deserialisierung schlägt fehl,

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

Die Anrufe werden auf die gleiche Umgebung, gleicher thread, gleiche Methode

        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

InformationsquelleAutor der Frage jackie | 2011-02-16

Schreibe einen Kommentar