Exemplo n.º 1
0
        /// <summary>
        ///   Serialize public properties of a Settings object to a given xml file
        /// </summary>
        /// <param name = "obj">Setting Object to serialize</param>
        /// <param name = "fileName">Xml file name</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";
            }
            NLog.Logger log = ServiceScope.Get <ILogger>().GetLogger;
            log.Trace("Serialize({0},{1})", obj.ToString(), fileName);
            Dictionary <string, string> globalSettingsList = new Dictionary <string, string>();
            Dictionary <string, string> userSettingsList   = new Dictionary <string, string>();
            XmlSettingsProvider         xmlWriter          = new XmlSettingsProvider(fileName);
            string fullFileName = String.Format(@"{0}\{1}", Options.ConfigDir, fileName);

            ;

            bool isFirstSave = (!File.Exists(fullFileName));

            foreach (PropertyInfo property in obj.GetType().GetProperties())
            {
                Type   thisType   = property.PropertyType;
                string defaultval = "";

                #region CLR Typed property

                if (isCLRType(thisType))
                {
                    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;

                    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>
        ///   Serialize public properties of a Settings object to a given xml file
        /// </summary>
        /// <param name = "obj">Setting Object to serialize</param>
        /// <param name = "fileName">Xml file name</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";
              }
              NLog.Logger log = ServiceScope.Get<ILogger>().GetLogger;
              log.Trace("Serialize({0},{1})", obj.ToString(), fileName);
              Dictionary<string, string> globalSettingsList = new Dictionary<string, string>();
              Dictionary<string, string> userSettingsList = new Dictionary<string, string>();
              XmlSettingsProvider xmlWriter = new XmlSettingsProvider(fileName);
              string fullFileName = String.Format(@"{0}\{1}", Options.ConfigDir, fileName);
              ;

              bool isFirstSave = (!File.Exists(fullFileName));
              foreach (PropertyInfo property in obj.GetType().GetProperties())
              {
            Type thisType = property.PropertyType;
            string defaultval = "";

            #region CLR Typed property

            if (isCLRType(thisType))
            {
              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;

              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
        }