By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,105 Members | 1,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,105 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ) );

}

}

}

Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Mike Pollett" <mp******@NoSpam.com> wrote in
news:ey*************@TK2MSFTNGP11.phx.gbl:
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

--
Solutions Design : http://www.sd.nl
My open source .NET Software : http://www.sd.nl/software
My .NET Blog : http://weblogs.asp.net/FBouma
-------------------------------------------------------------------------
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.