c# - XML version-specific deserialization -


so, have base project , several modified version of it. baseproject contains class baseclass,

namespace baseproject.someclasses{     public abstract class baseclass{     //...     } } 

while each of versions contain several inheritors of baseclass - f.e. projectone:

namespace baseproject.versionone.someclasses{     public class inheritorclass : baseclass{     //some logic here...     } } 

and projecttwo:

namespace baseproject.versiontwo.someclasses{     public class inheritorclass : baseclass{     //some different logic here...     } } 

the thing differs name of namespace.

base project loads each of assemblies during runtime , gets of inherited types. need create xml file, should contain both of inheritors' instances , pointers class instance should deserialized into:

... <baseclass xsi:type="inheritorclass"> <!-- versionone -->     <propone></propone>     <proptwo></proptwo>     <propthree></propthree>     <!-- ... --> </baseclass> <baseclass xsi:type="inheritorclass"> <!-- versiontwo -->     <propfour></propfour>     <propfive></propfive>     <propsix></propsix>     <!-- ... --> </baseclass> ... 

is there way deserialize xml (which contains instances of inheritors both of versions) ienumerable<baseclass>?

you need apply [xmltypeattribute(name)] attribute derived classes disambiguate xst:type names:

namespace baseproject.versionone.someclasses {     [xmltype("versiononeinheritorclass")]     public class inheritorclass : baseclass     {         public string versiononeproperty { get; set; } // instance     } } 

and

namespace baseproject.versiontwo.someclasses {     [xmltype("versiontwoinheritorclass")]     public class inheritorclass : baseclass     {         public string versiontwoproperty { get; set; } // instance     } } 

then xml appear follows, , can serialized , deserialized without loss of information:

<baseclass xsi:type="versiononeinheritorclass">     <versiononeproperty>one</versiononeproperty> </baseclass> <baseclass xsi:type="versiontwoinheritorclass">     <versiontwoproperty>two</versiontwoproperty> </baseclass> 

by way, if constructing xmlserializer using xmlserializer (type, type[]) constructor add in discovered derived types, must cache serializer in static cache somewhere, or else have horrible resource leak.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -