Exemplo n.º 1
0
        /// <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);

            NLog.Logger log = ServiceScope.Get <ILogger>().GetLogger;
            log.Trace("Deserialize({0},{1})", obj.ToString(), 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 (value == null || value == string.Empty)
                        {
                            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) {}
                }
                #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) {}
                    }
                }

                #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);
              NLog.Logger log = ServiceScope.Get<ILogger>().GetLogger;
              log.Trace("Deserialize({0},{1})", obj.ToString(), 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 (value == null || value == string.Empty) 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) {}
            }
              #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) {}
              }
            }

            #endregion
              }
        }