Пример #1
0
        public bool SaveTree <T>(LookupTree <T> tree) where T : class, IPayload
        {
            if (tree == null)
            {
                return(false);
            }

            try
            {
                string treePath = _pathGetPattern.Get <T>();
                string treeData = JsonConvert.SerializeObject(
                    tree,
                    new JsonSerializerSettings()
                {
                    TypeNameHandling     = TypeNameHandling.Auto,
                    DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
                    Formatting           = Formatting.Indented
                });
                if (string.IsNullOrWhiteSpace(treeData))
                {
                    throw new NullReferenceException("Tree data is null.");
                }

                return(_dataSetPattern.Set <T>(treePath, treeData));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Save tree failed. [{ex.GetType().Name}] {ex.Message}");
                return(false);
            }
        }
Пример #2
0
        public bool TryLoadTree <T>(out LookupTree <T> loadedTree) where T : class, IPayload
        {
            loadedTree = null;

            Type type = typeof(T);

            if (_cachedTrees.TryGetValue(type, out object obj) &&
                obj is LookupTree <T> tree)
            {
                loadedTree = tree;
                return(true);
            }

            try
            {
                string treePath = _pathGetPattern.Get <T>();
                string treeData = _dataGetPattern.Get <T>(treePath);
                if (string.IsNullOrWhiteSpace(treeData))
                {
                    throw new NullReferenceException("Tree data is null.");
                }

                loadedTree = JsonConvert.DeserializeObject <LookupTree <T> >(
                    treeData,
                    new JsonSerializerSettings()
                {
                    TypeNameHandling     = TypeNameHandling.Auto,
                    DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
                    Formatting           = Formatting.Indented
                });

                _cachedTrees[type] = loadedTree ?? throw new NullReferenceException("Loaded tree is null.");
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Load tree failed. [{ex.GetType().Name}] {ex.Message}");
                return(false);
            }
        }
Пример #3
0
 /// <summary>
 /// Tries to get a symbol at the specified location.
 /// </summary>
 /// <param name="position">The requested position.</param>
 /// <param name="symbol">The symbol that could be identified at the given position, or <c>null</c> if no symbol could be identified.</param>
 /// <returns><c>true</c> if a symbol was found, otherwise <c>false</c>.</returns>
 /// <exception cref="System.InvalidOperationException">Thrown if there was one more symbol at the specified position. This should never happen, unless there was an error.</exception>
 public bool TryGetSymbolAt(Position position, [NotNullWhen(true)] out ILocalizableSymbol?symbol)
 {
     symbol = LookupTree.Query(position).SingleOrDefault();
     return(symbol != null);
 }