示例#1
0
        /// <summary>Saves this dictionary to file</summary>
        /// <typeparam name="KeyType">Key type</typeparam>
        /// <typeparam name="ValueType">Value type</typeparam>
        /// <param name="dictionary">Dictionary to save</param>
        /// <param name="file">File to save to</param>
        /// <param name="keyConversion">Conversion from keys to strings</param>
        /// <param name="valueConversion">Conversion from values to strings</param>
        /// <param name="sort">How to sort keys before printing lines</param>
        /// <param name="reverse">Whether or not to reverse the sorted key collection</param>
        /// <param name="writeCount">Whether or not to write the dictionary's count to file</param>
        /// <param name="linePrefix">Key-value line prefix</param>
        /// <param name="keyValSeparator">Key-value sparator</param>
        /// <param name="throwExceptionOnContainsSeparator">Whether or not to throw an exception if the key or value string
        /// contains the separator sequence</param>
        public static void Save <KeyType, ValueType>(this Dictionary <KeyType, ValueType> dictionary, StreamWriter file, Func <KeyType, string> keyConversion, Func <ValueType, string> valueConversion, DictionaryExtensions.Sort sort, bool reverse, bool writeCount, string linePrefix, string keyValSeparator, bool throwExceptionOnContainsSeparator)
        {
            if (reverse && sort == DictionaryExtensions.Sort.None)
            {
                throw new Exception("Cannot reverse without sorting");
            }
            if (writeCount)
            {
                file.WriteLine(dictionary.Count);
            }
            IEnumerable <KeyType> keyTypes;

            if (sort != DictionaryExtensions.Sort.None)
            {
                KeyType[] array;
                if (sort == DictionaryExtensions.Sort.KeysByValues)
                {
                    array = dictionary.SortKeysByValues <KeyType, ValueType>();
                }
                else
                {
                    if (sort != DictionaryExtensions.Sort.Keys)
                    {
                        throw new NotImplementedException("Sort not implemented:  " + (object)sort);
                    }
                    array = dictionary.Keys.ToArray <KeyType>();
                    Array.Sort <KeyType>(array);
                }
                if (reverse)
                {
                    Array.Reverse((Array)array);
                }
                keyTypes = (IEnumerable <KeyType>)array;
            }
            else
            {
                keyTypes = (IEnumerable <KeyType>)dictionary.Keys;
            }
            foreach (KeyType index in keyTypes)
            {
                string str1 = keyConversion(index);
                string str2 = valueConversion(dictionary[index]);
                if (throwExceptionOnContainsSeparator && (str1.Contains(keyValSeparator) || str2.Contains(keyValSeparator)))
                {
                    throw new Exception("Neither key nor value can contain the key-value separater sequence");
                }
                file.WriteLine(linePrefix + str1 + keyValSeparator + str2);
            }
        }
示例#2
0
        /// <summary>Saves this dictionary to file</summary>
        /// <typeparam name="KeyType">Key type</typeparam>
        /// <typeparam name="ValueType">Value type</typeparam>
        /// <param name="dictionary">Dictionary to save</param>
        /// <param name="path">Path to save to</param>
        /// <param name="keyConversion">Conversion from keys to strings</param>
        /// <param name="valueConversion">Conversion from values to strings</param>
        /// <param name="sort">How to sort keys before printing lines</param>
        /// <param name="reverse">Whether or not to reverse the sorted key collection</param>
        /// <param name="writeCount">Whether or not to write the dictionary's count to file</param>
        /// <param name="linePrefix">Key-value line prefix</param>
        /// <param name="keyValSeparator">Key-value sparator</param>
        /// <param name="throwExceptionOnContainsSeparator">Whether or not to throw an exception if the key or value string
        /// contains the separator sequence</param>
        public static void Save <KeyType, ValueType>(this Dictionary <KeyType, ValueType> dictionary, string path, Func <KeyType, string> keyConversion, Func <ValueType, string> valueConversion, DictionaryExtensions.Sort sort, bool reverse, bool writeCount, string linePrefix, string keyValSeparator, bool throwExceptionOnContainsSeparator)
        {
            StreamWriter file = new StreamWriter(path);

            dictionary.Save <KeyType, ValueType>(file, keyConversion, valueConversion, sort, reverse, writeCount, linePrefix, keyValSeparator, throwExceptionOnContainsSeparator);
            file.Close();
        }