In an ISerializable class, how to 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 ) );

}

}

}
 

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