In an ISerializable class, how do you serialize / deserialize CollectionBase?

M

Mike Pollett

Hi, I have used the ISerializable interface before and the code below worked
fine. Until I derived it from CollectionBase. The code will still serialize
and deserialize the properties in this class and properties derived from
this class but will not serialize or deserialize the properties in
CollectionBase. Like InnerList, which is a read only property of
CollectionBase.

How can I serialize and deserialize the InnerList property of
CollectionBase?

Thanks

Mike

Code

public class SerialCollection : CollectionBase, ISerializable {

public SerialCollection () { }

protected SerialCollection ( SerializationInfo info, StreamingContext
context ) {

string exps = "";

// get the set of serializable members for our class and base classes

Type thisType = this.GetType();

MemberInfo[] mi = thisType.GetFields( BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance );

// DeSerialize all of the allowed fields and handle any of the removed
fields.

foreach ( MemberInfo member in mi ) {

try {

((FieldInfo)member).SetValue (this, info.GetValue( member.Name,
((FieldInfo)member).FieldType ));

} catch ( Exception e ) {

// resolve upgrade issues here

switch ( member.Name ) {

case "AddEvent":

// Handle the Event properties

continue;

}

exps += String.Format( "\nError during deserialization setting
Member name: {0} : {1}", member.Name, e.Message );

}

}

// this.InnerList = info.GetString("k");

}



void ISerializable.GetObjectData ( SerializationInfo info, StreamingContext
context ) {

// get the set of serializable members for our class and base classes

Type thisType = this.GetType();

MemberInfo[] mi = thisType.GetFields( BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance );

foreach ( MemberInfo member in mi ) {

info.AddValue( member.Name, ((FieldInfo)member).GetValue( this ) );

}

}

}
 
F

Frans Bouma

Hi, I have used the ISerializable interface before and the code below
worked fine. Until I derived it from CollectionBase. The code will still
serialize and deserialize the properties in this class and properties
derived from this class but will not serialize or deserialize the
properties in CollectionBase. Like InnerList, which is a read only
property of CollectionBase.

How can I serialize and deserialize the InnerList property of
CollectionBase?

The general approach is to first serialize a value with the name
'AmountItems' or something with the value of the amount of elements in
your list, then you loop through each element in the list and serialize
that using normal serialization techniques, thus add it as a normal
object. For the name of the object, use something like "item" +
loopcounter.

Deserialization is a bit trickier: you first grab the AmountItems
value and you then walk again through the items and now you read all items
back in in a variable using the name "item" + loopcounter, and call your
Add() overload (or the List.Add()) to add it to the list.

That's practically it.

FB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top