Add DictionaryExtensions

This commit is contained in:
Oliver Booth 2019-12-17 11:50:27 +00:00
parent b2455d1bcd
commit b0e5073b39
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,108 @@
namespace X10D
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
#endregion
/// <summary>
/// A set of extension methods for <see cref="Dictionary{TKey,TValue}"/>.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Converts a <see cref="IReadOnlyDictionary{T1,T2}"/> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{
static string SanitizeValue<T>(T value)
{
return
value is string str &&
Regex.IsMatch(str, "\\s")
? $"\"{str}\""
: value.ToString();
}
IEnumerable<string> strings =
dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}");
return String.Join(";", strings);
}
/// <summary>
/// Converts a <see cref="IDictionary{T1,T2}"/> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this IDictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>) dictionary).ToConnectionString();
}
/// <summary>
/// Converts a <see cref="Dictionary{T1,T2}"/> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this Dictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>) dictionary).ToConnectionString();
}
/// <summary>
/// Converts an <see cref="IReadOnlyDictionary{T1,T2}"/> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{
static string Sanitize(KeyValuePair<T1, T2> kvp)
{
string key = HttpUtility.UrlEncode(kvp.Key.ToString());
string value = HttpUtility.UrlEncode(kvp.Value.ToString());
return $"{key}={value}";
}
return String.Join("&", dictionary.Select(Sanitize));
}
/// <summary>
/// Converts an <see cref="IDictionary{T1,T2}"/> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this IDictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>) dictionary).ToGetParameters();
}
/// <summary>
/// Converts a <see cref="Dictionary{T1,T2}"/> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="String"/> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this Dictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>) dictionary).ToGetParameters();
}
}
}

View File

@ -24,3 +24,4 @@ Below is a list of the number of extension methods written for a given type. Ove
| `IList<T>` | `X10D` | 2 |
| `Random` | `X10D` | 2 |
| `string` / `SecureString` | `X10D` | 8 |
| `Dictionary<T1, T2>` | 2 |