/// <summary> /// Returns a <see cref="string" /> that represents this given hashset to be stored as a string. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="hashset">The hashset.</param> /// <param name="info">The information.</param> /// <param name="getString">The get string.</param> /// <returns> /// A <see cref="string" /> that represents this instance. /// </returns> public static string ToString <T>(HashSet <T> hashset, ConfigKeyValueSplitInfo info = null, Func <T, string> getString = null) { info = info ?? ConfigKeyValuesSplitInfo.Default; getString = getString ?? ToStringDefault; return(string.Join(info.EntrySeperators.First().ToString(), hashset.Select(s => getString(s)).Select(s => info.ConvertValuesToLower ? s.ToLower() : s))); }
private static Dictionary <TKey, TValue> GetDictionary <TKey, TValue>(ConfigKeyValueSplitInfo info, string config) { info = info ?? ConfigKeyValueSplitInfo.Default; return(config.Split(info.EntrySeperators, StringSplitOptions.RemoveEmptyEntries). Select(entry => entry.Split(info.KeyValueSeperators, StringSplitOptions.RemoveEmptyEntries)). ToDictionary(values => info.ParseKey <TKey>(values[0]), values => info.ParseValue <TValue>(values.Length > 1 ? values[1] : null))); }
/// <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}:{Value}|{Key}:{Value} /// </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, TValue> GetDictionary <TKey, TValue>(string appSetting, string defaultValue, ConfigKeyValueSplitInfo info = null) { try { return(GetDictionary <TKey, TValue>(info, ConfigProvider.Instance[appSetting] ?? defaultValue)); } catch (Exception ex) { throw new FormatException($"Error occured parsing dictionary for app setting \"{appSetting}\", with defaultValue, \"{defaultValue}\"", ex); } }
/// <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}:{Value}|{Key}:{Value} /// </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, TValue> GetDictionary <TKey, TValue>(string appSetting, Dictionary <TKey, TValue> defaultValue, ConfigKeyValueSplitInfo info = null) { try { var config = ConfigProvider.Instance[appSetting]; return(config == null ? defaultValue : GetDictionary <TKey, TValue>(info, config)); } catch (Exception ex) { throw new FormatException($"Error occured parsing dictionary for app setting \"{appSetting}\"", ex); } }
/// <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}:{Value}|{Key}:{Value} /// </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, TValue> GetDictionary <TKey, TValue>(string appSetting, Func <Dictionary <TKey, TValue> > getDefault, ConfigKeyValueSplitInfo info = null) { try { var config = ConfigurationManager.AppSettings[appSetting]; return(config == null?getDefault() : GetDictionary <TKey, TValue>(info, config)); } catch (Exception ex) { throw new FormatException($"Error occured parsing dictionary for app setting \"{appSetting}\"", ex); } }
/// <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, TValue> dictionary, ConfigKeyValueSplitInfo info = null, Func <TKey, string> getKeyString = null, Func <TValue, string> getValueString = null) { info = info ?? ConfigKeyValueSplitInfo.Default; getKeyString = getKeyString ?? ToStringDefault; getValueString = getValueString ?? ToStringDefault; var values = from kvp in dictionary let key = getKeyString(kvp.Key) let value = getValueString(kvp.Value) select $"{(info.ConvertKeysToLower ? key.ToLower() : key)}{info.KeyValueSeperators.First()}{(info.ConvertValuesToLower ? value.ToLower() : value)}"; return(string.Join(info.EntrySeperators.First().ToString(), values)); }
/// <summary> /// Attempts to read the setting from the config file, and Parse to get the value. /// 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 setting should be in the format {Key}:{Value}|{Key}:{Value} /// </summary> /// <typeparam name="T"></typeparam> /// <param name="appSetting">The application setting.</param> /// <param name="key">AppSetting Key attribute value.</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 T GetAppSettingOrDefaultByKey <T>(string appSetting, string key, T defaultValue, ConfigKeyValueSplitInfo info = null) { try { var config = ConfigProvider.Instance[appSetting]; if (config == null) { return(defaultValue); } info = info ?? ConfigKeyValueSplitInfo.Default; // Find the Key:Value where the Key Matches config = config.Split(info.EntrySeperators). FirstOrDefault(v => v.Split(info.KeyValueSeperators). Select(k => info.ParseKey <string>(k)). FirstOrDefault() == info.ParseKey <string>(key)); return(config == null ? defaultValue : SubstringByString(config, info.KeyValueSeperators.ToString()).ParseOrConvertString <T>()); } catch (Exception ex) { throw new FormatException($"Error occured processing app setting \"{appSetting}\" for key \"{key}\" with default value \"{defaultValue}\"", ex); } }