Example #1
0
        /// <summary>
        /// Returns a <see cref="string" /> that represents the given dictionary object to be stored as a string
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="info">The information.</param>
        /// <param name="getKeyString">The get key string.</param>
        /// <param name="getValueString">The get value string.</param>
        /// <returns>
        /// A <see cref="string" /> that represents the given dictionary object to be stored as a string
        /// </returns>
        public static string ToString <TKey, TValue>(Dictionary <TKey, HashSet <TValue> > dictionary,
                                                     ConfigKeyValuesSplitInfo info        = null,
                                                     Func <TKey, string> getKeyString     = null,
                                                     Func <TValue, string> getValueString = null)
        {
            info           = info ?? ConfigKeyValuesSplitInfo.Default;
            getKeyString   = getKeyString ?? ToStringDefault;
            getValueString = getValueString ?? ToStringDefault;

            var values = (from kvp in dictionary
                          let key = getKeyString(kvp.Key)
                                    let prefix = (info.ConvertKeysToLower ? key.ToLower() : key) + info.KeyValueSeperators.First()
                                                 let items = kvp.Value.Select(v => getValueString(v)).Select(v => info.ConvertValuesToLower ? v.ToLower() : v)
                                                             select prefix + string.Join(info.EntryValuesSeperators.First().ToString(), items)).ToList();

            return(string.Join(info.EntrySeperators.First().ToString(), values));
        }
Example #2
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="getDefault">Function to get the default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(string appSetting,
                                                                                     Func <Dictionary <TKey, HashSet <TValue> > > getDefault,
                                                                                     ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigProvider.Instance[appSetting];
         return(config == null?getDefault() : GetDictionaryHash <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary Hash for app setting \"{appSetting}\"", ex);
     }
 }
Example #3
0
        private static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(ConfigKeyValuesSplitInfo info, string config)
        {
            info = info ?? ConfigKeyValuesSplitInfo.Default;
            var dict = new Dictionary <TKey, HashSet <TValue> >();

            foreach (var entry in config.Split(info.EntrySeperators, StringSplitOptions.RemoveEmptyEntries))
            {
                var entryValues = entry.Split(info.KeyValueSeperators, StringSplitOptions.RemoveEmptyEntries);
                var value       = entryValues.Length > 1
                    ? new HashSet <TValue>(entryValues[1].Split(info.EntryValuesSeperators, StringSplitOptions.RemoveEmptyEntries).Select(info.ParseValue <TValue>))
                    : new HashSet <TValue>();
                dict.Add(info.ParseKey <TKey>(entryValues[0]), value);
            }

            return(dict);
        }
Example #4
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="defaultValue">The default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(string appSetting,
                                                                                     string defaultValue,
                                                                                     ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigProvider.Instance[appSetting] ?? defaultValue;
         return(GetDictionaryHash <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary Hash for app setting \"{appSetting}\", with default value \"{defaultValue}\"", ex);
     }
 }
Example #5
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="defaultValue">The default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, List <TValue> > GetDictionaryList <TKey, TValue>(string appSetting,
                                                                                  Dictionary <TKey, List <TValue> > defaultValue,
                                                                                  ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigProvider.Instance[appSetting];
         return(config == null ? defaultValue : GetDictionaryList <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary list for app setting \"{appSetting}\"", ex);
     }
 }
Example #6
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="defaultValue">The default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, HashSet <TValue> > GetDictionaryHash <TKey, TValue>(string appSetting,
                                                                                     Dictionary <TKey, HashSet <TValue> > defaultValue,
                                                                                     ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigurationManager.AppSettings[appSetting];
         return(config == null ? defaultValue : GetDictionaryHash <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary Hash for app setting \"{appSetting}\"", ex);
     }
 }
Example #7
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="getDefault">Function to get the default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, List <TValue> > GetDictionaryList <TKey, TValue>(string appSetting,
                                                                                  Func <Dictionary <TKey, List <TValue> > > getDefault,
                                                                                  ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigurationManager.AppSettings[appSetting];
         return(config == null?getDefault() : GetDictionaryList <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary list for app setting \"{appSetting}\"", ex);
     }
 }
Example #8
0
 /// <summary>
 /// Attempts to read the setting from the config file, and Parse into a Dictionary.
 /// If the Type doesn't contain a Parse, a cast is attempted.
 /// Any failure in the Parse will throw an exception.
 /// If the config value is null, then the default value will be used.
 /// The default setting should be in the format {Key}:{Value1},{value2}|{Key}:{Value1}
 /// </summary>
 /// <typeparam name="TKey">The type of the key.</typeparam>
 /// <typeparam name="TValue">The type of the value.</typeparam>
 /// <param name="appSetting">The application setting.</param>
 /// <param name="defaultValue">The default value.</param>
 /// <param name="info">The settings by which to split the config value.</param>
 /// <returns></returns>
 public static Dictionary <TKey, List <TValue> > GetDictionaryList <TKey, TValue>(string appSetting,
                                                                                  string defaultValue,
                                                                                  ConfigKeyValuesSplitInfo info = null)
 {
     try
     {
         var config = ConfigurationManager.AppSettings[appSetting] ?? defaultValue;
         return(GetDictionaryList <TKey, TValue>(info, config));
     }
     catch (Exception ex)
     {
         throw new FormatException($"Error occured parsing dictionary list for app setting \"{appSetting}\", with default value \"{defaultValue}\"", ex);
     }
 }