Serialization and derived classes

  • Thread starter Thread starter StuartM
  • Start date Start date
S

StuartM

Can anybody point me towards a sample of serializing/deserializing specific
classes that are derived from a generic class? Is it necessary to use a
switch statement or can it be done in a more extensible way?
 
Stuart,

You shouldn't have a problem with this. The type information is stored
in the stream, and the formatter should be able to pick this up.

All you have to do in your derived class is attach the Serializable
attribute and optionally implement the ISerializable interface.

Hope this helps.
 
Stuart,

You shouldn't have a problem with this. The type information is stored
in the stream, and the formatter should be able to pick this up.

All you have to do in your derived class is attach the Serializable
attribute and optionally implement the ISerializable interface.

Hope this helps.

Perhaps I'm making this harder than it really is. AIUI when I deserialize
the object I have to cast it to its correct type, if I don't know which
derived class I've got how do I do the cast?
 
Hi Stuart,

Do you really need to know the type of object if they all derive from the
same base class? Surely polymorphism will take care the various
sub-classes?

If you really do need to know the type you could check with the 'is' keyword
and cast accordingly, but that's ugly and you'll have to update it for any
new sub-classes created.

Though it must be said I'm no serialization expert so there may be a cleaner
way...

--Liam.
 
Back
Top