public virtual void ImportSettings(object import) { try { // Try convert settingsCache = (Dictionary <string, object>)import; // Serialize string serialized = JsonConvert.SerializeObject(settingsCache, Formatting.Indented); // Write to file UnsafeNativeHelpers.WriteStringToFile(settingsPath, serialized); } catch (Exception ex) { Debug.WriteLine(ex); Debugger.Break(); } }
protected virtual TValue Get <TValue>(TValue defaultValue, [CallerMemberName] string propertyName = "") { try { // Check if caching is enabled if (isCachingEnabled) { // If the cache contains the setting... if (settingsCache.ContainsKey(propertyName)) { TValue settingValue; // Get the object object settingObject = settingsCache[propertyName]; // Check if it's a JToken object if (settingObject is JToken jtoken) { // Get the value from JToken settingValue = jtoken.ToObject <TValue>(); } else { // Otherwise, it is TValue, get the value settingValue = (TValue)settingObject; } // Return the setting and exit this function return(settingValue); } // Cache miss, the cache doesn't contain the setting, continue, to update the cache } // Read all settings from file string settingsData = UnsafeNativeHelpers.ReadStringFromFile(settingsPath); // If there are existing settings... if (!string.IsNullOrEmpty(settingsData)) { // Deserialize them and update the cache settingsCache = JsonConvert.DeserializeObject <Dictionary <string, object> >(settingsData); } // If it doesn't have this setting... if (!settingsCache.ContainsKey(propertyName)) { // Add it to cache settingsCache.Add(propertyName, defaultValue); // Serialize with updated value string serialized = JsonConvert.SerializeObject(settingsCache, Formatting.Indented); // Write to file UnsafeNativeHelpers.WriteStringToFile(settingsPath, serialized); } // Get the value object object valueObject = settingsCache[propertyName]; if (valueObject is JToken jtoken2) { return(jtoken2.ToObject <TValue>()); } return((TValue)valueObject); } catch (Exception ex) { Debug.WriteLine(ex); Debugger.Break(); return(default);