示例#1
0
        private async Task <JsonSrnDictionary> GetCacheDictAsync(string nameSpace, bool createIfNotExist = false)
        {
            if (_cache.ContainsKey(nameSpace))
            {
                return(_cache[nameSpace]);
            }

            // Store not in the cache, we need to load it from the filesystem.
            var path = BuildPath(nameSpace);

            if (File.Exists(path))
            {
                _cache[nameSpace] = JsonConvert.DeserializeObject <JsonSrnDictionary>(
                    await File.ReadAllTextAsync(BuildPath(nameSpace)), new DynamicDictConverter <JsonSrnDictionary>());
            }
            else if (createIfNotExist)
            {
                _cache[nameSpace] = new JsonSrnDictionary();
            }
            else
            {
                return(null);
            }

            return(_cache[nameSpace]);
        }
        internal new dynamic this[string key]
        {
            get
            {
                // Dot paths use nested dictionaries
                var idx = key.IndexOf('.');
                if (idx != -1)
                {
                    var firstKey = key.Substring(0, idx);

                    // Find our nested dictionary
                    if (!ContainsKey(firstKey) || base[firstKey].GetType() != typeof(JsonSrnDictionary))
                    {
                        return(null);
                    }
                    var str = key.Substring(idx + 1);
                    return(base[firstKey][str]);
                }

                return(ContainsKey(key) ? base[key] : null);
            }
            set
            {
                // Dot paths use nested dictionaries
                var idx = key.IndexOf('.');
                if (idx != -1)
                {
                    var firstKey = key.Substring(0, idx);

                    // Create our nested dictionary
                    if (!ContainsKey(firstKey) || base[firstKey].GetType() != typeof(JsonSrnDictionary))
                    {
                        base[firstKey] = new JsonSrnDictionary();
                    }
                    base[firstKey][key.Substring(idx + 1)] = value;

                    return;
                }

                if (value == null)
                {
                    // Setting the value to null should remove it.
                    Remove(key);
                    return;
                }

                base[key] = value;
            }
        }