/// <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(); } }
/// <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); } } }
/// <summary> /// Registers an instance with the SettingsManager, linking up any auto-update events. /// </summary> protected void RegisterInstance(IPersistable instance) { Type t = instance.GetType(); // Retreive the type's cache, or create one if it does not already exist. if (!typeCache.ContainsKey(t)) { typeCache.Add(t, new TypeReflectionCache(instance)); } TypeReflectionCache cache = typeCache[t]; // Hook up any existing auto-update event handlers for this instance. foreach (TypeReflectionCacheItem item in cache.Properties) { if (item.EventInfo != null) { item.EventInfo.AddEventHandler(instance, new EventHandler(UpdateHandler)); } } }