Click to See Complete Forum and Search --> : Serialization, why does this work?


EEKstream
July 27th, 2006, 12:12 AM
I have an object, we can call it ClassA, that are to be serialized. It's containing an arraylist. It looks something like this


[Serializable()]
public class ClassA {
private ArrayList list;
public ArrayList List {
// get/set
}

public override void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("list", list);
}
}

and also a constructor for the deserialization. The thing is that I store objects of type ClassB in my ArrayList. I've marked this class (ClassB) as Serializable but it does not implement ISerializable or has a GetObjectData method. Now the question is: Why is this working? How can the ArrayList populated with ClassB-objects be serialized when there is no serialize-method for ClassB? Because everything works fine and when I deserialize my object, the arraylist contains the same stuff as it did before the serialization.

jhammer
July 27th, 2006, 02:42 AM
When you mark a class with the Serializable attribute you don't need to implement the interace ISerializable. The default behavior will occur when there is a need to serialize or deserialize the object.
If you need a special behavior then you should implement the interface.

EEKstream
July 27th, 2006, 03:15 AM
Which is the default behavior? Because I have other classes, and I need to implement ISerializable in them or otherwise I will get the wrong values (e.g. if a value is true, after serialization/deserialization, it will be false and so on)..but it works on some classes without..

hansipet
July 27th, 2006, 03:25 AM
you need to implement this only if you want to serialize data, which is not pubilc or if you have to save some data which isn't serializable.

normally on bool value this should work without problems. maybe you have some other errors or problems in your code...which serializer do you use?

Regards
Hansjörg

EEKstream
July 27th, 2006, 04:18 AM
I found it, it had nothing to do with the serialization. Thanks, this saved me a lot of code :)