public void UpdateAll(UISettings nui) { foreach (PropertyInfo info in nui.GetType().GetProperties()) { //if (info.CanRead) object o = propertyInfo.GetValue(myObject, null); if (info.CanWrite) { info.SetValue(this, info.GetValue(nui)); } } }
//public static T DeepCopy<T>(T other) //{ // using (var ms = new MemoryStream()) { // var formatter = new BinaryFormatter(); // formatter.Serialize(ms, other); // ms.Position=0; // return (T)formatter.Deserialize(ms); // } //} //public static async Task<bool> Save(this UISettings ui, string filename = "LastSaved.xsetting") //{ // ui.ShowBusySign("Saving Settings..."); // bool result = await Task<bool>.Factory.StartNew(() => { // try { // using (FileStream fsUserSetting = File.Create(filename)) { // var formatter = new XmlSerializer(ui.GetType()); // formatter.Serialize(fsUserSetting, ui); // } // return true; // } catch { MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } // }); // ui.StopBusySign(); return result; //} public static async Task <bool> SaveCrypt(this UISettings ui, string filename = "LastSaved.xsetting") { ui.ShowBusySign("Saving Settings..."); try { await Task.Factory.StartNew(() => { var aUE = new UnicodeEncoding(); byte[] key = aUE.GetBytes("password"); var RMCrypto = new RijndaelManaged(); using (var fs = File.Open(filename, FileMode.Create)) { using (var cs = new CryptoStream(fs, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) { var xml = new XmlSerializer(ui.GetType()); xml.Serialize(cs, ui); } } }); return(true); } catch { MessageBox.Show("Failed to save the current settings!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return(false); } finally { ui.StopBusySign(); } }