IDictionaryExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Masuit.Tools
  5. {
  6. public static class IDictionaryExtensions
  7. {
  8. /// <summary>
  9. /// 添加或更新键值对
  10. /// </summary>
  11. /// <typeparam name="TKey"></typeparam>
  12. /// <typeparam name="TValue"></typeparam>
  13. /// <param name="this"></param>
  14. /// <param name="key">键</param>
  15. /// <param name="value">值</param>
  16. /// <returns></returns>
  17. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue value)
  18. {
  19. if ([email protected](key))
  20. {
  21. @this.Add(new KeyValuePair<TKey, TValue>(key, value));
  22. }
  23. else
  24. {
  25. @this[key] = value;
  26. }
  27. return @this[key];
  28. }
  29. /// <summary>
  30. /// 添加或更新键值对
  31. /// </summary>
  32. /// <typeparam name="TKey"></typeparam>
  33. /// <typeparam name="TValue"></typeparam>
  34. /// <param name="this"></param>
  35. /// <param name="that">另一个字典集</param>
  36. /// <returns></returns>
  37. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  38. {
  39. foreach (var item in that)
  40. {
  41. AddOrUpdate(@this, item.Key, item.Value);
  42. }
  43. }
  44. /// <summary>
  45. /// 添加或更新键值对
  46. /// </summary>
  47. /// <typeparam name="TKey"></typeparam>
  48. /// <typeparam name="TValue"></typeparam>
  49. /// <param name="this"></param>
  50. /// <param name="that">另一个字典集</param>
  51. /// <returns></returns>
  52. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  53. {
  54. foreach (var item in @this)
  55. {
  56. AddOrUpdate(that, item.Key, item.Value);
  57. }
  58. }
  59. /// <summary>
  60. /// 添加或更新键值对
  61. /// </summary>
  62. /// <typeparam name="TKey"></typeparam>
  63. /// <typeparam name="TValue"></typeparam>
  64. /// <param name="this"></param>
  65. /// <param name="key">键</param>
  66. /// <param name="addValue">添加时的值</param>
  67. /// <param name="updateValueFactory">更新时的操作</param>
  68. /// <returns></returns>
  69. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  70. {
  71. if ([email protected](key))
  72. {
  73. @this.Add(new KeyValuePair<TKey, TValue>(key, addValue));
  74. }
  75. else
  76. {
  77. @this[key] = updateValueFactory(key, @this[key]);
  78. }
  79. return @this[key];
  80. }
  81. /// <summary>
  82. /// 添加或更新键值对
  83. /// </summary>
  84. /// <typeparam name="TKey"></typeparam>
  85. /// <typeparam name="TValue"></typeparam>
  86. /// <param name="this"></param>
  87. /// <param name="that">另一个字典集</param>
  88. /// <param name="updateValueFactory">更新时的操作</param>
  89. /// <returns></returns>
  90. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  91. {
  92. foreach (var item in that)
  93. {
  94. AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
  95. }
  96. }
  97. /// <summary>
  98. /// 添加或更新键值对
  99. /// </summary>
  100. /// <typeparam name="TKey"></typeparam>
  101. /// <typeparam name="TValue"></typeparam>
  102. /// <param name="this"></param>
  103. /// <param name="that">另一个字典集</param>
  104. /// <param name="updateValueFactory">更新时的操作</param>
  105. /// <returns></returns>
  106. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  107. {
  108. foreach (var item in @this)
  109. {
  110. AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
  111. }
  112. }
  113. /// <summary>
  114. /// 添加或更新键值对
  115. /// </summary>
  116. /// <typeparam name="TKey"></typeparam>
  117. /// <typeparam name="TValue"></typeparam>
  118. /// <param name="this"></param>
  119. /// <param name="key">键</param>
  120. /// <param name="addValueFactory">添加时的操作</param>
  121. /// <param name="updateValueFactory">更新时的操作</param>
  122. /// <returns></returns>
  123. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  124. {
  125. if ([email protected](key))
  126. {
  127. @this.Add(new KeyValuePair<TKey, TValue>(key, addValueFactory(key)));
  128. }
  129. else
  130. {
  131. @this[key] = updateValueFactory(key, @this[key]);
  132. }
  133. return @this[key];
  134. }
  135. /// <summary>
  136. /// 遍历IEnumerable
  137. /// </summary>
  138. /// <param name="dic"></param>
  139. /// <param name="action">回调方法</param>
  140. public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dic, Action<TKey, TValue> action)
  141. {
  142. foreach (var item in dic)
  143. {
  144. action(item.Key, item.Value);
  145. }
  146. }
  147. /// <summary>
  148. /// 安全的转换成字典集
  149. /// </summary>
  150. /// <typeparam name="TSource"></typeparam>
  151. /// <typeparam name="TKey"></typeparam>
  152. /// <typeparam name="TElement"></typeparam>
  153. /// <param name="source"></param>
  154. /// <param name="keySelector">键选择器</param>
  155. /// <param name="elementSelector">值选择器</param>
  156. /// <returns></returns>
  157. public static Dictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  158. {
  159. var dic = new Dictionary<TKey, TElement>();
  160. foreach (var item in source)
  161. {
  162. AddOrUpdate(dic, keySelector(item), elementSelector(item));
  163. }
  164. return dic;
  165. }
  166. /// <summary>
  167. /// 安全的转换成字典集
  168. /// </summary>
  169. /// <typeparam name="TSource"></typeparam>
  170. /// <typeparam name="TKey"></typeparam>
  171. /// <typeparam name="TElement"></typeparam>
  172. /// <param name="source"></param>
  173. /// <param name="keySelector">键选择器</param>
  174. /// <param name="elementSelector">值选择器</param>
  175. /// <returns></returns>
  176. public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  177. {
  178. var dic = new ConcurrentDictionary<TKey, TElement>();
  179. foreach (var item in source)
  180. {
  181. AddOrUpdate(dic, keySelector(item), elementSelector(item));
  182. }
  183. return dic;
  184. }
  185. /// <summary>
  186. /// 转换成并发字典集合
  187. /// </summary>
  188. /// <typeparam name="TKey"></typeparam>
  189. /// <typeparam name="TValue"></typeparam>
  190. /// <param name="dic"></param>
  191. /// <returns></returns>
  192. public static ConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic)
  193. {
  194. var cd = new ConcurrentDictionary<TKey, TValue>();
  195. cd.AddOrUpdate(dic);
  196. return cd;
  197. }
  198. /// <summary>
  199. /// 转换成普通字典集合
  200. /// </summary>
  201. /// <typeparam name="TKey"></typeparam>
  202. /// <typeparam name="TValue"></typeparam>
  203. /// <param name="dic"></param>
  204. /// <returns></returns>
  205. public static Dictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic)
  206. {
  207. var cd = new Dictionary<TKey, TValue>();
  208. cd.AddOrUpdate(dic);
  209. return cd;
  210. }
  211. }
  212. }