// Internal methods ---------------------------------------------------- protected virtual void UpdateMemberVariables(PersistentXML persistentXML) { foreach (PropertyInfo toProperty in this.GetType().GetProperties()) { // do not replace special "FileName" property with persisted value if (toProperty.Name != "FileName") { toProperty.SetValue(this, persistentXML.GetType().GetProperty(toProperty.Name).GetValue(persistentXML, null), null); } } }
// Deserializes the class from the config file. public bool Load() { XmlSerializer mySerializer = null; FileStream myFileStream = null; bool fileExists = false; try { // Create an XmlSerializer for the PersistentXML type. mySerializer = new XmlSerializer(this.GetType()); FileInfo fi = new FileInfo(fileName); // If the config file exists, open it. if (fi.Exists) { myFileStream = fi.OpenRead(); // Create a new instance of the PersistentXML by // deserializing the config file. PersistentXML myAppSettings = (PersistentXML)mySerializer.Deserialize(myFileStream); UpdateMemberVariables(myAppSettings); fileExists = true; } } // catch(Exception ex) // { // MessageBox.Show(ex.Message); // } finally { // If the FileStream is open, close it. if (myFileStream != null) { myFileStream.Close(); } } return(fileExists); }