Generically Referring to a Complex Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have built several kinds of complex classes that I work with in my program.
Storing them to disk is no problem because I just pass the instantiated
object to a SaveData method, accepting it as a generic "object". Then I use
reflection to iterate through the object and save the data.

But loading the data from disk seems to be anything but generic. I have
several challenges with this but here's one of the first:

public void OpenData (string filename, object dataModel)
{
Poll modelPoll = new Poll();
SysInfo modelSys = new SysInfo();

switch(dataModel.GetType().Name)
{
case "Poll":
modelPoll = (Poll) dataModel;
break;

case "SysInfo":
modelSys = (SysInfo) dataModel;
break;
}

// ... much more code here
}


Because I need to get into the details of each complex object, I found that
I needed to cast the generic object to the specific object type first. But
this requires a whole lot of un-generic code.

Two questions:
1. Is there a way around this?
2. Is there an entirely different approach I should be considering?

Note re #2: My storage format is XML. But I endlessly tried to get the
built-in XML reader methods working to no avail. I *think* this is because
my complex objects have several nested collections and nested classes. I
need them to best map out what I'm doing so I found it necessary to write my
own Save & Open XML parsers. They work fine but I just wish I could make
them more reusable.
 
Peter, I don't understand your response whatsoever.

I think he's recommending that you have a look at the "serialization" topic
in the help file. The .Net framework already provides the ability to
serialize/deserialize objects (e.g. to/from disk). I've never used it, so I
can't say how easy it is or how well it works, but it's probably worth
looking into.
 
As I mentioned at the end of my original posting, I already extensively tried
using the built-in serialization but it didn't work.

Any thoughts about my two questions?
 
FYI,

The XmlSerializer does not handle circular references in an object graph you
would have to use the SoapFormatter class to serialize to Xml but this I
believe is being depricated in the next version of .Net.

HTH

Ollie Riches
 
Robert W. said:
I have built several kinds of complex classes that I work with in my program.
Storing them to disk is no problem because I just pass the instantiated
object to a SaveData method, accepting it as a generic "object". Then I use
reflection to iterate through the object and save the data.

But loading the data from disk seems to be anything but generic.
Because I need to get into the details of each complex object, I found that
I needed to cast the generic object to the specific object type first. But
this requires a whole lot of un-generic code.

Do all of your "complex objects" derive from a common ancestor that you
created? If so, is it possible for this common ancestor to add some
"genericness" to the process?
 
Hi Robert -

When you say Serialization didnt work, you didnt mention why.
When I first tried it, I wrestled with it until I discovered that
one needs two objects to make it work: the formatter, and the
stream.

After declaring my class [Serializable], I save my objects to a
binary file using this code:

MyObject obj = new MyObject( args );

IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream( "MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None );

formatter.Serialize( stream, obj );

stream.Close();

I would expect the XML formatter to work similarly. The compiler
and the CLR are the things that do all the work; it only took me
the lines of code shown.

Deserialization is done equally simply:
IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream( "MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read );

MyObject obj = (MyObject) formatter.Deserialize( stream );

stream.Close();

Hope that helps

--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
 

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

Back
Top