c# - How to overwrite default collection on deserialization -
i have class represents bunch of settings:
public class settings { public settings() { innersettings = new list<innersettings>(); //this settings want have default innersettings.add(new innersettings()); } [xmlarray] public list<innersettings> innersettings {get; set;} }
now, when deserialize object, innersettings
xml added list
on top of items created in default settings()
constructor. there easy way tell deserializer, want overwrite collection instead, removing default items?
xml serializer doesn't support callback , deriving xmlserializer
class there few methods can override. let's see (dirty) workarounds:
1) change default constructor (the 1 xmlserializer
call) not include default item(s). add second constructor dummy parameter , expose factory method:
public class settings { private settings() { // called xmldeserializer } private settings(int dummy) { // called factory method innersettings = new list<innersettings> { new inndersettings(); } } public static settings create() { return new settings(0); } }
2) remove items after object has been deserialized. create own xmlserializer
this:
public class settingsxmlserializer : xmlserializer { protected override object deserialize(xmlserializationreader reader) { var settings = (settings)base.deserialize(reader); settings.innersettings.removeat(0); return settings; } }
3) should right way it's not easy complex types: long job of ixmlserializable
implementation...
Comments
Post a Comment