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.
--
Robert W.
Vancouver, BC
www.mwtech.com