protected virtual TValue Get <TValue>(TValue defaultValue, [CallerMemberName] string propertyName = "")
        {
            try
            {
                string settingsData = NativeFileOperationsHelper.ReadStringFromFile(settingsPath);

                Dictionary <string, TValue> rawData       = JsonConvert.DeserializeObject <Dictionary <string, TValue> >(settingsData);
                Dictionary <string, object> convertedData = new Dictionary <string, object>();

                if (rawData != null)
                {
                    foreach (var item in rawData)
                    {
                        convertedData.Add(item.Key, (TValue)item.Value);
                    }
                }

                serializableSettings = convertedData;

                if (serializableSettings == null)
                {
                    serializableSettings = new Dictionary <string, object>();
                }

                if (!serializableSettings.ContainsKey(propertyName))
                {
                    serializableSettings.Add(propertyName, defaultValue);

                    // Serialize
                    NativeFileOperationsHelper.WriteStringToFile(settingsPath, JsonConvert.SerializeObject(serializableSettings, Formatting.Indented));
                }

                return((TValue)serializableSettings[propertyName]);
            }
            catch (Exception e)
            {
                Debugger.Break();
                return(default(TValue));
            }
        }
        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 = NativeFileOperationsHelper.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
                    NativeFileOperationsHelper.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);
示例#3
0
 public static string ReadFileTag(string filePath)
 {
     return(NativeFileOperationsHelper.ReadStringFromFile($"{filePath}:files"));
 }
示例#4
0
 public string ReadFromFile()
 {
     return(NativeFileOperationsHelper.ReadStringFromFile(_filePath));
 }