1

Lets say, I have a class with objects in it.

namespace Class_Serialization
{
    [Serializable]
    public class Data
    {
        public string Name = "Example1";
        public string place = "Torino";
        public DateTime time = DateTime.Now;
    }
}

I am trying to serialize it using ISerialization interface

[Serializable]
public class SerializeThisClass : ISerializable
{
    public Data StreamThisData;

    public SerializeThisClass()
    {

    }

    public SerializeThisClass(Data _StreamThisData)
    {
        StreamThisData = _StreamThisData;
    }
    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Object Data", StreamThisData);
    }
}

Now, When I serialize with the code below

Data DataToSerialize = new Data();
BinaryFormatter DataToBinary = new BinaryFormatter();
SerializeThisClass serialize = new SerializeThisClass(DataToSerialize);
SerializeIn SerializeInMem = new SerializeIn();
DataToBinary.Serialize(SerializeInMem.StreamOfData, serialize);
ListOfStreams.Add(SerializeInMem);

It serializes normally, but when I try to deserialize it, it gives me error

BinaryFormatter BinaryToData = new BinaryFormatter();
foreach (SerializeIn x in ListOfStreams)
{
    x.StreamOfData.Position = 0;
    SerializeThisClass DeserializeData = (SerializeThisClass)BinaryToData.Deserialize(x.StreamOfData);
    MessageBox.Show("Name: " + DeserializeData.StreamThisData.Name + "\nPlace: " + DeserializeData.StreamThisData.place + "\nDateTime: " + DeserializeData.StreamThisData.time.ToString());
}

Error: $exception{"The constructor to deserialize an object of type 'Class_Serialization.SerializeThisClass' was not found."} System.Exception {System.Runtime.Serialization.SerializationException}

Justin
  • 84,773
  • 49
  • 224
  • 367
Jasim Khan Afridi
  • 776
  • 3
  • 15
  • 28
  • Try do it this way http://stackoverflow.com/questions/5017274/c-sharp-binaryformatter-and-deserialization-complex-objects – Michał Powaga Nov 21 '11 at 12:09
  • Frankly, given that all you are doing is adding a single element, I'd be tempted to remove `ISerializable` and just let `BinaryFormatter` handle it automatically. – Marc Gravell Nov 21 '11 at 12:11

2 Answers2

4

If you are implementing ISerializable, you need a constructor of the signature:

protected YourType(SerializationInfo information, StreamingContext context) {}

which loads the data (basically, the reverse of GetObjectData). Presumably, with (untested):

StreamThisData = (Data)info.GetValue("Object Data", typeof(Data));
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 Although I knew what was missing, damned if I could remember the explanation! lol – Adam Houldsworth Nov 21 '11 at 12:09
  • @JasimKhanAfridi Your `GetObjectData` is used when serializing, the constructor is used when deserializing. As Marc says, it should simply do the reverse and pull values out of the container into your class. – Adam Houldsworth Nov 21 '11 at 12:10
  • 1
    @JasimKhanAfridi in all seriousness, you almost certainly don't need to actually implement ISerializable here. Just sayin' – Marc Gravell Nov 21 '11 at 12:22
  • @JasimKhanAfridi second what Marc almost all of the `ISerializable` implementations I've seen are written by new c# developers that don't understand how serialization attributes work yet. You almost certainly don't need it here. If stock serialization isn't doing it for you look at `OnDeserializedAttribute` before implementing ISerializable. – Yaur Nov 21 '11 at 12:43
1

Try adding the constructor:

protected SerializeThisClass(SerializationInfo info, StreamingContext context)
{
}

http://msdn.microsoft.com/en-us/library/ms182343(v=vs.80).aspx

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187