| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- namespace Masuit.Tools
- {
- public static class IDictionaryExtensions
- {
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="key">键</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue value)
- {
- if ([email protected](key))
- {
- @this.Add(new KeyValuePair<TKey, TValue>(key, value));
- }
- else
- {
- @this[key] = value;
- }
- return @this[key];
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="that">另一个字典集</param>
- /// <returns></returns>
- public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
- {
- foreach (var item in that)
- {
- AddOrUpdate(@this, item.Key, item.Value);
- }
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="that">另一个字典集</param>
- /// <returns></returns>
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
- {
- foreach (var item in @this)
- {
- AddOrUpdate(that, item.Key, item.Value);
- }
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="key">键</param>
- /// <param name="addValue">添加时的值</param>
- /// <param name="updateValueFactory">更新时的操作</param>
- /// <returns></returns>
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
- {
- if ([email protected](key))
- {
- @this.Add(new KeyValuePair<TKey, TValue>(key, addValue));
- }
- else
- {
- @this[key] = updateValueFactory(key, @this[key]);
- }
- return @this[key];
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="that">另一个字典集</param>
- /// <param name="updateValueFactory">更新时的操作</param>
- /// <returns></returns>
- public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
- {
- foreach (var item in that)
- {
- AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
- }
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="that">另一个字典集</param>
- /// <param name="updateValueFactory">更新时的操作</param>
- /// <returns></returns>
- public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
- {
- foreach (var item in @this)
- {
- AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
- }
- }
- /// <summary>
- /// 添加或更新键值对
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="this"></param>
- /// <param name="key">键</param>
- /// <param name="addValueFactory">添加时的操作</param>
- /// <param name="updateValueFactory">更新时的操作</param>
- /// <returns></returns>
- public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
- {
- if ([email protected](key))
- {
- @this.Add(new KeyValuePair<TKey, TValue>(key, addValueFactory(key)));
- }
- else
- {
- @this[key] = updateValueFactory(key, @this[key]);
- }
- return @this[key];
- }
- /// <summary>
- /// 遍历IEnumerable
- /// </summary>
- /// <param name="dic"></param>
- /// <param name="action">回调方法</param>
- public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dic, Action<TKey, TValue> action)
- {
- foreach (var item in dic)
- {
- action(item.Key, item.Value);
- }
- }
- /// <summary>
- /// 安全的转换成字典集
- /// </summary>
- /// <typeparam name="TSource"></typeparam>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TElement"></typeparam>
- /// <param name="source"></param>
- /// <param name="keySelector">键选择器</param>
- /// <param name="elementSelector">值选择器</param>
- /// <returns></returns>
- public static Dictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- {
- var dic = new Dictionary<TKey, TElement>();
- foreach (var item in source)
- {
- AddOrUpdate(dic, keySelector(item), elementSelector(item));
- }
- return dic;
- }
- /// <summary>
- /// 安全的转换成字典集
- /// </summary>
- /// <typeparam name="TSource"></typeparam>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TElement"></typeparam>
- /// <param name="source"></param>
- /// <param name="keySelector">键选择器</param>
- /// <param name="elementSelector">值选择器</param>
- /// <returns></returns>
- public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
- {
- var dic = new ConcurrentDictionary<TKey, TElement>();
- foreach (var item in source)
- {
- AddOrUpdate(dic, keySelector(item), elementSelector(item));
- }
- return dic;
- }
- /// <summary>
- /// 转换成并发字典集合
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="dic"></param>
- /// <returns></returns>
- public static ConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic)
- {
- var cd = new ConcurrentDictionary<TKey, TValue>();
- cd.AddOrUpdate(dic);
- return cd;
- }
- /// <summary>
- /// 转换成普通字典集合
- /// </summary>
- /// <typeparam name="TKey"></typeparam>
- /// <typeparam name="TValue"></typeparam>
- /// <param name="dic"></param>
- /// <returns></returns>
- public static Dictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic)
- {
- var cd = new Dictionary<TKey, TValue>();
- cd.AddOrUpdate(dic);
- return cd;
- }
- }
- }
|