Serialization of NameObjectCollectionBase descendents

  • Thread starter Thread starter Sam Marrocco
  • Start date Start date
S

Sam Marrocco

I've created a collection class that inherits NameObjectCollectionBase
(call it MyCollection). MyCollection is declared as serializable, and
serializes fine. A cursory glance into the serialized data seems to
contain everything in MyCollection and it's base class data, but when I
attempt to deserialize it, I get errors about not being able to find the
deserialization constructor.

Is something not being "passed through" from the inherited
NameObjectCollectionBase into MyCollection class that is preventing
deserialization? I can't seem to find any methods or properties in
NameObjectCollectionBase that indicate deserialization, so where would
they be? I could see implementing an IFormatter interface in
MyCollection, but what would I call in it's methods?


-Sam
 
Sam,
It appears that NameObjectCollectionBase implements the ISerializable
interface.

When a base class implements ISerializable it basically means your class
needs to "implement" the same contract, not with the Implements keyword, as
the base already has, but with the proper overloaded constructor &
overridable method, which are:

Imports System.Runtime.Serialization

Protected Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
MyBase.New(info, context)

End Sub

Public Overrides Sub GetObjectData(ByVal info As SerializationInfo,
ByVal context As StreamingContext)

End Sub

The constructor can be protected or public, I normally make both protected,
as classes outside of serialization have no "real" business calling them.
Note the designer of the base class decides if GetObjectData is protected or
public. Hopefully the designer also made it overridable ;-)

You use the methods on the SerializationInfo to get & set name/value pairs
that will be serialized. For details see:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
 
Thanks, Jay. I'm going to spend some time reading those articles and try
to get into it deeper!

Sam,
It appears that NameObjectCollectionBase implements the ISerializable
interface.

--
==================================================================
Sam J. Marrocco
Sr. Visual Effects Artist/R&D
Travelling Pictures/GTN
Inferno, Flame, Maya, All that cool stuff!
"The fact that no one understands you doesn't make you an artist."
==================================================================
 
Back
Top