mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 00:05:42 +00:00
✨ Add DictionaryExtensions
This commit is contained in:
parent
b2455d1bcd
commit
b0e5073b39
108
X10D/DictionaryExtensions.cs
Normal file
108
X10D/DictionaryExtensions.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,3 +24,4 @@ Below is a list of the number of extension methods written for a given type. Ove
|
|||||||
| `IList<T>` | `X10D` | 2 |
|
| `IList<T>` | `X10D` | 2 |
|
||||||
| `Random` | `X10D` | 2 |
|
| `Random` | `X10D` | 2 |
|
||||||
| `string` / `SecureString` | `X10D` | 8 |
|
| `string` / `SecureString` | `X10D` | 8 |
|
||||||
|
| `Dictionary<T1, T2>` | 2 |
|
Loading…
Reference in New Issue
Block a user