/// <summary> /// Serialize public properties of a Settings object to a given xml file /// </summary> /// <param name = "obj">Setting Object to serialize</param> public static void Serialize(object obj) { string fileName = ""; INamedSettings namedSettings = obj as INamedSettings; if (namedSettings != null) { fileName = obj + "." + namedSettings.Name + ".xml"; } else { fileName = obj + ".xml"; } var options = (ServiceLocator.Current.GetInstance(typeof(ISettingsManager)) as ISettingsManager).GetOptions; var log = (ServiceLocator.Current.GetInstance(typeof(ILogger)) as ILogger).GetLogger; log.Trace($"Serialize({ obj},{fileName})"); var globalSettingsList = new Dictionary <string, string>(); var userSettingsList = new Dictionary <string, string>(); var xmlWriter = new XmlSettingsProvider(fileName); var fullFileName = $@"{options.ConfigDir}\{fileName}"; bool isFirstSave = (!File.Exists(fullFileName)); foreach (var property in obj.GetType().GetProperties()) { var thisType = property.PropertyType; var defaultval = ""; #region CLR Typed property if (isCLRType(thisType)) { object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); SettingScope scope; if (attributes.Length != 0) { var attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } string value = defaultval; if (!isFirstSave) //else default value will be used if it exists { if (obj.GetType().GetProperty(property.Name).GetValue(obj, null) != null) { value = obj.GetType().GetProperty(property.Name).GetValue(obj, null).ToString(); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } else { globalSettingsList.Add(property.Name, value); } } else { if (scope == SettingScope.Global) { globalSettingsList.Add(property.Name, value); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } } } #endregion #region not CLR Typed property else { XmlSerializer xmlSerial = new XmlSerializer(thisType); StringBuilder sb = new StringBuilder(); StringWriter strWriter = new StringWriter(sb); XmlTextWriter writer = new XmlNoNamespaceWriter(strWriter); writer.Formatting = Formatting.Indented; object propertyValue = obj.GetType().GetProperty(property.Name).GetValue(obj, null); xmlSerial.Serialize(writer, propertyValue); strWriter.Close(); strWriter.Dispose(); // remove unneeded encoding tag sb.Remove(0, 41); object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); SettingScope scope = SettingScope.Global; if (attributes.Length != 0) { SettingAttribute attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } string value = defaultval; /// a changer if (!isFirstSave || defaultval == "") { value = sb.ToString(); } if (scope == SettingScope.User) { userSettingsList.Add(property.Name, value); } else { globalSettingsList.Add(property.Name, value); } } #endregion } #region write Settings // write settings to xml foreach (KeyValuePair <string, string> pair in globalSettingsList) { xmlWriter.SetValue(obj.ToString(), pair.Key, pair.Value, SettingScope.Global); } foreach (KeyValuePair <string, string> pair in userSettingsList) { xmlWriter.SetValue(obj.ToString(), pair.Key, pair.Value, SettingScope.User); } xmlWriter.Save(); #endregion }
/// <summary> /// De-serialize public properties of a Settings object from a given xml file /// </summary> /// <param name = "obj">Setting Object to retrieve</param> /// <param name = "fileName">Xml file name</param> public static void Deserialize(object obj) { string fileName = ""; INamedSettings namedSettings = obj as INamedSettings; if (namedSettings != null) { fileName = obj + "." + namedSettings.Name + ".xml"; } else { fileName = obj + ".xml"; } XmlSettingsProvider xmlreader = new XmlSettingsProvider(fileName); var options = (ServiceLocator.Current.GetInstance(typeof(ISettingsManager)) as ISettingsManager).GetOptions; var log = (ServiceLocator.Current.GetInstance(typeof(ILogger)) as ILogger).GetLogger; log.Trace($"Deserialize({obj},{fileName})"); // if xml file doesn't exist yet then create it string fullFileName = String.Format(@"{0}\{1}", options.ConfigDir, fileName); ; if (!File.Exists(fullFileName)) { Serialize(obj); } foreach (PropertyInfo property in obj.GetType().GetProperties()) { Type thisType = property.PropertyType; #region get scope SettingScope scope = SettingScope.Global; object[] attributes = property.GetCustomAttributes(typeof(SettingAttribute), false); string defaultval = ""; if (attributes.Length != 0) { SettingAttribute attribute = (SettingAttribute)attributes[0]; scope = attribute.SettingScope; defaultval = attribute.DefaultValue; } else { scope = SettingScope.Global; defaultval = ""; } #endregion if (isCLRType(thisType)) #region CLR Typed property { try { string value = xmlreader.GetValue(obj.ToString(), property.Name, scope); if (string.IsNullOrEmpty(value)) { value = defaultval; } if (thisType == typeof(string)) { property.SetValue(obj, value, null); } if (thisType == typeof(bool)) { property.SetValue(obj, bool.Parse(value), null); } if (thisType == typeof(Int16)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(Int64)) { property.SetValue(obj, Int64.Parse(value), null); } if (thisType == typeof(UInt16)) { property.SetValue(obj, UInt16.Parse(value), null); } if (thisType == typeof(UInt32)) { property.SetValue(obj, UInt32.Parse(value), null); } if (thisType == typeof(UInt64)) { property.SetValue(obj, UInt64.Parse(value), null); } if (thisType == typeof(float)) { property.SetValue(obj, float.Parse(value), null); } if (thisType == typeof(double)) { property.SetValue(obj, double.Parse(value), null); } if (thisType == typeof(Int16)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(DateTime)) { property.SetValue(obj, DateTime.Parse(value), null); } if (thisType == typeof(bool?)) { property.SetValue(obj, bool.Parse(value), null); } if (thisType == typeof(Int16?)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32?)) { property.SetValue(obj, Int32.Parse(value), null); } if (thisType == typeof(Int64?)) { property.SetValue(obj, Int64.Parse(value), null); } if (thisType == typeof(UInt16?)) { property.SetValue(obj, UInt16.Parse(value), null); } if (thisType == typeof(UInt32?)) { property.SetValue(obj, UInt32.Parse(value), null); } if (thisType == typeof(UInt64?)) { property.SetValue(obj, UInt64.Parse(value), null); } if (thisType == typeof(float?)) { property.SetValue(obj, float.Parse(value), null); } if (thisType == typeof(double?)) { property.SetValue(obj, double.Parse(value), null); } if (thisType == typeof(Int16?)) { property.SetValue(obj, Int16.Parse(value), null); } if (thisType == typeof(Int32?)) { property.SetValue(obj, Int32.Parse(value), null); } } catch (Exception) { // ignored } } #endregion else #region not CLR Typed property { XmlSerializer xmlSerial = new XmlSerializer(thisType); string value = xmlreader.GetValue(obj.ToString(), property.Name, scope); if (value != null) { TextReader reader = new StringReader(value); try { property.SetValue(obj, xmlSerial.Deserialize(reader), null); } catch (Exception) { // ignored } } } #endregion } }