I am generating classes from XSD
and need to populate the classes to serialize to xml.
I have different classes, containing all the info that goes into the classes generated.
The problem is that the generated classes come in versions, and properties in those classes are other classes in the same version.
class LocalData
{
public MyClass property { get; set; }
}
class XmlVersion1
{
public MyClassV1 property { get; set; }
}
class XmlVersion2
{
public MyClassV2 property { get; set; }
public MyClassXV2 newProperty { get; set; }
}
The data in MyClassV1
and V2
are basically the same, so the same code can be used.
I wanted to make a factory that just took the LocalData
class and any of the versioned classes and populated the data in the versioned class, but I run into a problem when I want to do property = new MyClassVx
, because the factory does not know which version it's supposed to create.
I could do
if (parameter is MyClassV1)
paramter.MyClassV1 = new MyClassV1
and so on, but that is a LOT of code.
This is for generating xml messages that are specified by an external company, and they come in different versions, and we have to be able to serialize and deserialize the content into our internal system.
We have not found a solution to this specific issue and chose to use AutoMapper which seems to solve our problem in a different way.
We made a tool that takes the generated classes and creates the mapping classes needed for AutoMapper through assembly. If you have large generated classes you could do this as well. We can now create thousands of lines of code needed to map classes. It solves an issue we had when mapping types of 'object' to specific classes. I don't know if it's helpful but there it is.