Serializing/deserializing a List<> object

A

Andrew Falanga

Hi all,

I have a use case where a list of classes are deserialized to an XML
and serialized from that XML after a system reboot and the program
starts again. The class contained in this list look like this:

class SomeData {
string name;
int size;
}

The class is deserialized and serialized from a generic pointer/
reference in another class which is typed simply as "object." The
deserialization works just fine, and so does the serialization after
the program starts up again. What isn't happening is when I assign
the serialized data, after reboot, to the typed object that is
actually used.

So, for example, I have this:

class SomeClass {
List<SomeData> persistentData;

SomeClass( ) {
if(reentrant)
persistentData = ProgramConfiguration.Instance.Data as
List<SomeData>; // error here
else
persistentData = new List<SomeData>();
}
}

The program doesn't fail anywhere except for when persistentData is
used *after* a reboot. After the reboot, that line produces a null
persistentData object. Why is that?

Originally, the program needed only to know about one SomeData object
and so all that was serialized was one object. That line, where
assignment of the data is done to the "persistentData" object worked,
as you see above. Now, however, a list must be stored and retrieved
from disk. I'm updating the code to accommodate this need, but
although the ProgramConfiguration.Instance.Data object contains that
list, the assignment using as isn't working right and the
persistentData object is null when done.

Any help is greatly appreciated.

Andy
 
A

Andrew Falanga

Andrew said:
[...]
So, for example, I have this:
class SomeClass {
   List<SomeData> persistentData;
   SomeClass( ) {
      if(reentrant)
         persistentData = ProgramConfiguration.Instance.Data as
List<SomeData>; // error here
      else
         persistentData = new List<SomeData>();
   }
}
The program doesn't fail anywhere except for when persistentData is
used *after* a reboot.  After the reboot, that line produces a null
persistentData object.  Why is that?

Impossible to say without a concise-but-complete code example that
reliably demonstrates the problem.  But fundamentally, somehow the
deserialized object that is produced from the XML does not wind up being
an instance of List<SomeData>.

In the meantime, you may be interested to know that "serialization" is
the conversion of your run-time objects _to_ some other format (e.g.
XML), and "deserialization" is the conversion of that format back to the
equivalent run-time objects.

Pete

Pete,

Thanks for the response. Thanks for the clarification of
serialization and deserialization. As you know, I was confused.

Andy
 

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