I have a settings class that I want to be able to serialize and deserialize to save and load settings to a file.
With hindsight, I'd have designed my whole app to create an instance of settings when needed. As it is, my settings are persistent (mimicking the default application settings behaviour). Given my current architecture, I can do the following:
public void Save()
{
Stream stream = File.Open("settings_test.xml", FileMode.Create);
XmlSerializer x = new XmlSerializer(this.GetType());
x.Serialize(stream, this);
}
Obviously, I can't do this:
public void Load()
{
Stream stream = File.Open("settings_test.xml", FileMode.Open);
XmlSerializer x = new XmlSerializer(this.GetType());
this = x.Deserialize(stream);
}
But is there a way to update all the public properties of the current class to match those of the deserialized object?
(I suppose the more general questions is: "Is there an efficient way to update one class based on the contents of another?")