static Settings create(SettingsMemberInfo settingsFieldInfo, bool reset, bool throwExceptionIfCouldNotLoadFromStorageFile) { if (!reset && File.Exists(settingsFieldInfo.File)) { try { return((Settings)Cliver.Serialization.Json.Load(settingsFieldInfo.Type, settingsFieldInfo.File, true, true)); } catch (Exception e) { if (throwExceptionIfCouldNotLoadFromStorageFile) { throw new Exception("Error while loading settings " + settingsFieldInfo.FullName + " from file " + settingsFieldInfo.File, e); } } } if (File.Exists(settingsFieldInfo.InitFile)) { FileSystemRoutines.CopyFile(settingsFieldInfo.InitFile, settingsFieldInfo.File, true); try { return((Settings)Cliver.Serialization.Json.Load(settingsFieldInfo.Type, settingsFieldInfo.InitFile, true, true)); } catch (Exception e) { throw new Exception("Error while loading settings " + settingsFieldInfo.FullName + " from initial file " + settingsFieldInfo.InitFile, e); } } return((Settings)Activator.CreateInstance(settingsFieldInfo.Type)); }
/// <summary> /// Can be used to initialize an optional Settings field. /// </summary> /// <param name="settingsFieldFullName">full name of Settings field; it equals to the name of its storage file without extention</param> /// <param name="throwExceptionIfCouldNotLoadFromStorageFile"></param> public static void Reload(string settingsFieldFullName, bool throwExceptionIfCouldNotLoadFromStorageFile = false) { SettingsMemberInfo sfi = GetSettingsFieldInfo(settingsFieldFullName); Settings s = Settings.Create(sfi, false, throwExceptionIfCouldNotLoadFromStorageFile); sfi.SetObject(s); }
//// ???what would it be needed for? //public static S CreateResetInstance<S>(string settingsFieldFullName) where S : Settings, new() //{ // return (S)Settings.Create(GetSettingsFieldInfo(settingsFieldFullName), true, true); //} //// ???what would it be needed for? //public static S CreateReloadedInstance<S>(string settingsFieldFullName, bool throwExceptionIfCouldNotLoadFromStorageFile = false) where S : Settings, new() //{ // return (S)Settings.Create(GetSettingsFieldInfo(settingsFieldFullName), false, throwExceptionIfCouldNotLoadFromStorageFile); //} /// <summary> /// Can be used to initialize an optional Settings field. /// </summary> /// <param name="settingsFieldFullName">full name of Settings field; it equals to the name of its storage file without extention</param> public static void Reset(string settingsFieldFullName) { SettingsMemberInfo sfi = GetSettingsFieldInfo(settingsFieldFullName); Settings s = Settings.Create(sfi, true, true); sfi.SetObject(s); }
internal static Settings Create(SettingsMemberInfo settingsFieldInfo, bool reset, bool throwExceptionIfCouldNotLoadFromStorageFile) { Settings settings = create(settingsFieldInfo, reset, throwExceptionIfCouldNotLoadFromStorageFile); settings.__Info = settingsFieldInfo; settings.Loaded(); return(settings); }
internal void Save(SettingsMemberInfo settingsFieldInfo)//avoids a redundant check and provides an appropriate exception message { lock (this) { if (__Info != settingsFieldInfo)//which can only happen if there are several settings fields of the same type { throw new Exception("The value of Settings field '" + settingsFieldInfo.FullName + "' is not attached to it."); } save(); } }