Exemple #1
0
        private static Dictionary <TKey, TValue> RetrieveDictionaryGeneric <TKey, TValue>(Node node)
        {
            bool keyIsBase = BaseTypesManager.IsBaseType(typeof(TKey));

            var dictionary = new Dictionary <TKey, TValue>(capacity: node.ChildNodes.Count);

            if (keyIsBase && node.ChildNodeType == NodeChildrenType.key)
            {
                foreach (var child in node.ChildNodes)
                {
                    string childKey = (child as KeyNode).Key;
                    var    key      = BaseTypesManager.ParseBaseType <TKey>(childKey);
                    var    value    = NodeManager.GetNodeData <TValue>(child);
                    dictionary.Add(key, value);
                }
            }
            else
            {
                var array = NodeManager.GetNodeData <WritableKeyValuePair <TKey, TValue>[]>(node);
                foreach (var kvp in array)
                {
                    dictionary.Add(kvp.key, kvp.value);
                }
            }

            return(dictionary);
        }
        /// <summary> Interpret this file as a dictionary. Top-level keys in the file are interpreted as keys in the dictionary. </summary>
        /// <remarks> TKey must be a Base Type </remarks>
        public Dictionary <TKey, TValue> GetAsDictionary <TKey, TValue>()
        {
            if (!BaseTypesManager.IsBaseType(typeof(TKey)))
            {
                throw new Exception("When using GetAsDictionary, TKey must be a base type");
            }

            var keys       = this.TopLevelKeys;
            var dictionary = new Dictionary <TKey, TValue>(capacity: keys.Count);

            foreach (var keyText in keys)
            {
                TKey   key   = BaseTypesManager.ParseBaseType <TKey>(keyText);
                TValue value = NodeManager.GetNodeData <TValue>(TopLevelNodes[keyText]);
                dictionary.Add(key, value);
            }

            return(dictionary);
        }