Пример #1
0
        /// <summary>
        /// Saves the specified instance the the options file(s).
        /// </summary>
        public void SaveInstance(IPersistable persistableObject)
        {
            Type t = persistableObject.GetType();

            if (!typeCache.ContainsKey(t))
            {
                RegisterInstance(persistableObject);
            }

            TypeReflectionCache cache = typeCache[t];

            foreach (TypeReflectionCacheItem item in cache.Properties)
            {
                bool    created;
                XmlNode valueNode = OptionsMerger.LocateOrCreateNode(item, persistableObject, out created);
                object  newValue  = item.PropertyInfo.GetValue(persistableObject, null);
                OptionsMerger.SetNodeValue(valueNode, newValue.ToString(), item.PersistanceInformation.Encrypted);
            }

            // Don't save the files to disk if we are currently in batch update mode.
            if (!batchInProgress)
            {
                SaveFiles();
            }
        }
Пример #2
0
        /// <summary>
        /// Loads the specified instance from the options file(s).
        /// </summary>
        public void LoadInstance(IPersistable persistableObject)
        {
            Type t = persistableObject.GetType();

            if (!typeCache.ContainsKey(t))
            {
                RegisterInstance(persistableObject);
            }

            TypeReflectionCache cache = typeCache[t];

            foreach (TypeReflectionCacheItem item in cache.Properties)
            {
                bool    created;
                XmlNode valueNode = LocateOrCreateNode(item, persistableObject, out created);
                string  value     = valueNode.InnerText;
                if (item.PersistanceInformation.Encrypted)
                {
                    value = OptionsMerger.DecryptString(value);
                }

                Type propertyType = item.PropertyInfo.PropertyType;
                if (propertyType == typeof(string))
                {
                    item.PropertyInfo.SetValue(persistableObject, value, null);
                }
                else if (propertyType == typeof(Single))
                {
                    item.PropertyInfo.SetValue(persistableObject, Convert.ToSingle(value), null);
                }
                else if (propertyType == typeof(int))
                {
                    item.PropertyInfo.SetValue(persistableObject, Convert.ToInt32(value), null);
                }
                else if (propertyType == typeof(bool))
                {
                    item.PropertyInfo.SetValue(persistableObject, Convert.ToBoolean(value), null);
                }
                else
                {
                    throw new Exception("An unknown type was encountered while loading named instance '" +
                                        persistableObject.InstanceName + "' - " + propertyType);
                }

                if (created)
                {
                    SaveInstance(persistableObject);
                }
            }
        }
Пример #3
0
 private SettingsManager(string systemSettingsFilename, string userSettingsFilename)
 {
     OptionsMerger.Initialize(userSettingsFilename, systemSettingsFilename);
 }
Пример #4
0
 /// <summary>
 /// Locates the XML node that contains persistance information for the specified item.
 /// If the node is not found, it will be created.
 /// </summary>
 private static XmlNode LocateOrCreateNode(TypeReflectionCacheItem item, IPersistable instance, out bool created)
 {
     return(OptionsMerger.LocateOrCreateNode(item, instance, out created));
 }
Пример #5
0
 /// <summary>
 /// Writes the XML documents to disk.
 /// </summary>
 private void SaveFiles()
 {
     OptionsMerger.Save();
 }