IDictionaryExtensions.cs 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. using Masuit.Tools.Systems;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace Masuit.Tools
  8. {
  9. public static class IDictionaryExtensions
  10. {
  11. /// <summary>
  12. /// 添加或更新键值对
  13. /// </summary>
  14. /// <typeparam name="TKey"></typeparam>
  15. /// <typeparam name="TValue"></typeparam>
  16. /// <param name="this"></param>
  17. /// <param name="that">另一个字典集</param>
  18. /// <returns></returns>
  19. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  20. {
  21. foreach (var item in that)
  22. {
  23. @this[item.Key] = item.Value;
  24. }
  25. }
  26. /// <summary>
  27. /// 添加或更新键值对
  28. /// </summary>
  29. /// <typeparam name="TKey"></typeparam>
  30. /// <typeparam name="TValue"></typeparam>
  31. /// <param name="this"></param>
  32. /// <param name="that">另一个字典集</param>
  33. /// <returns></returns>
  34. public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  35. {
  36. foreach (var item in that)
  37. {
  38. @this[item.Key] = item.Value;
  39. }
  40. }
  41. /// <summary>
  42. /// 添加或更新键值对
  43. /// </summary>
  44. /// <typeparam name="TKey"></typeparam>
  45. /// <typeparam name="TValue"></typeparam>
  46. /// <param name="this"></param>
  47. /// <param name="that">另一个字典集</param>
  48. /// <returns></returns>
  49. public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  50. {
  51. foreach (var item in that)
  52. {
  53. @this[item.Key] = item.Value;
  54. }
  55. }
  56. /// <summary>
  57. /// 添加或更新键值对
  58. /// </summary>
  59. /// <typeparam name="TKey"></typeparam>
  60. /// <typeparam name="TValue"></typeparam>
  61. /// <param name="this"></param>
  62. /// <param name="that">另一个字典集</param>
  63. /// <returns></returns>
  64. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  65. {
  66. foreach (var item in @this)
  67. {
  68. that[item.Key] = item.Value;
  69. }
  70. }
  71. /// <summary>
  72. /// 添加或更新键值对
  73. /// </summary>
  74. /// <typeparam name="TKey"></typeparam>
  75. /// <typeparam name="TValue"></typeparam>
  76. /// <param name="this"></param>
  77. /// <param name="that">另一个字典集</param>
  78. /// <returns></returns>
  79. public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  80. {
  81. foreach (var item in @this)
  82. {
  83. that[item.Key] = item.Value;
  84. }
  85. }
  86. /// <summary>
  87. /// 添加或更新键值对
  88. /// </summary>
  89. /// <typeparam name="TKey"></typeparam>
  90. /// <typeparam name="TValue"></typeparam>
  91. /// <param name="this"></param>
  92. /// <param name="that">另一个字典集</param>
  93. /// <returns></returns>
  94. public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  95. {
  96. foreach (var item in @this)
  97. {
  98. that[item.Key] = item.Value;
  99. }
  100. }
  101. /// <summary>
  102. /// 添加或更新键值对
  103. /// </summary>
  104. /// <typeparam name="TKey"></typeparam>
  105. /// <typeparam name="TValue"></typeparam>
  106. /// <param name="this"></param>
  107. /// <param name="key">键</param>
  108. /// <param name="addValue">添加时的值</param>
  109. /// <param name="updateValueFactory">更新时的操作</param>
  110. /// <returns></returns>
  111. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  112. {
  113. if ([email protected](key))
  114. {
  115. @this.Add(key, addValue);
  116. }
  117. else
  118. {
  119. @this[key] = updateValueFactory(key, @this[key]);
  120. }
  121. return @this[key];
  122. }
  123. /// <summary>
  124. /// 添加或更新键值对
  125. /// </summary>
  126. /// <typeparam name="TKey"></typeparam>
  127. /// <typeparam name="TValue"></typeparam>
  128. /// <param name="this"></param>
  129. /// <param name="key">键</param>
  130. /// <param name="addValue">添加时的值</param>
  131. /// <param name="updateValueFactory">更新时的操作</param>
  132. /// <returns></returns>
  133. public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  134. {
  135. if ([email protected](key))
  136. {
  137. @this.Add(key, addValue);
  138. }
  139. else
  140. {
  141. @this[key] = updateValueFactory(key, @this[key]);
  142. }
  143. return @this[key];
  144. }
  145. /// <summary>
  146. /// 添加或更新键值对
  147. /// </summary>
  148. /// <typeparam name="TKey"></typeparam>
  149. /// <typeparam name="TValue"></typeparam>
  150. /// <param name="this"></param>
  151. /// <param name="key">键</param>
  152. /// <param name="addValue">添加时的值</param>
  153. /// <param name="updateValueFactory">更新时的操作</param>
  154. /// <returns></returns>
  155. public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  156. {
  157. if ([email protected](key))
  158. {
  159. @this.TryAdd(key, addValue);
  160. }
  161. else
  162. {
  163. @this[key] = updateValueFactory(key, @this[key]);
  164. }
  165. return @this[key];
  166. }
  167. /// <summary>
  168. /// 添加或更新键值对
  169. /// </summary>
  170. /// <typeparam name="TKey"></typeparam>
  171. /// <typeparam name="TValue"></typeparam>
  172. /// <param name="this"></param>
  173. /// <param name="key">键</param>
  174. /// <param name="addValue">添加时的值</param>
  175. /// <param name="updateValueFactory">更新时的操作</param>
  176. /// <returns></returns>
  177. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  178. {
  179. if ([email protected](key))
  180. {
  181. @this.Add(key, addValue);
  182. }
  183. else
  184. {
  185. @this[key] = await updateValueFactory(key, @this[key]);
  186. }
  187. return @this[key];
  188. }
  189. /// <summary>
  190. /// 添加或更新键值对
  191. /// </summary>
  192. /// <typeparam name="TKey"></typeparam>
  193. /// <typeparam name="TValue"></typeparam>
  194. /// <param name="this"></param>
  195. /// <param name="key">键</param>
  196. /// <param name="addValue">添加时的值</param>
  197. /// <param name="updateValueFactory">更新时的操作</param>
  198. /// <returns></returns>
  199. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  200. {
  201. if ([email protected](key))
  202. {
  203. @this.Add(key, addValue);
  204. }
  205. else
  206. {
  207. @this[key] = await updateValueFactory(key, @this[key]);
  208. }
  209. return @this[key];
  210. }
  211. /// <summary>
  212. /// 添加或更新键值对
  213. /// </summary>
  214. /// <typeparam name="TKey"></typeparam>
  215. /// <typeparam name="TValue"></typeparam>
  216. /// <param name="this"></param>
  217. /// <param name="key">键</param>
  218. /// <param name="addValue">添加时的值</param>
  219. /// <param name="updateValueFactory">更新时的操作</param>
  220. /// <returns></returns>
  221. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  222. {
  223. if ([email protected](key))
  224. {
  225. @this.TryAdd(key, addValue);
  226. }
  227. else
  228. {
  229. @this[key] = await updateValueFactory(key, @this[key]);
  230. }
  231. return @this[key];
  232. }
  233. /// <summary>
  234. /// 添加或更新键值对
  235. /// </summary>
  236. /// <typeparam name="TKey"></typeparam>
  237. /// <typeparam name="TValue"></typeparam>
  238. /// <param name="this"></param>
  239. /// <param name="key">键</param>
  240. /// <param name="addValue">添加时的值</param>
  241. /// <param name="updateValue">更新时的值</param>
  242. /// <returns></returns>
  243. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
  244. {
  245. if ([email protected](key))
  246. {
  247. @this.Add(key, addValue);
  248. }
  249. else
  250. {
  251. @this[key] = updateValue;
  252. }
  253. return @this[key];
  254. }
  255. /// <summary>
  256. /// 添加或更新键值对
  257. /// </summary>
  258. /// <typeparam name="TKey"></typeparam>
  259. /// <typeparam name="TValue"></typeparam>
  260. /// <param name="this"></param>
  261. /// <param name="key">键</param>
  262. /// <param name="addValue">添加时的值</param>
  263. /// <param name="updateValue">更新时的值</param>
  264. /// <returns></returns>
  265. public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
  266. {
  267. if ([email protected](key))
  268. {
  269. @this.Add(key, addValue);
  270. }
  271. else
  272. {
  273. @this[key] = updateValue;
  274. }
  275. return @this[key];
  276. }
  277. /// <summary>
  278. /// 添加或更新键值对
  279. /// </summary>
  280. /// <typeparam name="TKey"></typeparam>
  281. /// <typeparam name="TValue"></typeparam>
  282. /// <param name="this"></param>
  283. /// <param name="key">键</param>
  284. /// <param name="addValue">添加时的值</param>
  285. /// <param name="updateValue">更新时的值</param>
  286. /// <returns></returns>
  287. public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
  288. {
  289. if ([email protected](key))
  290. {
  291. @this.TryAdd(key, addValue);
  292. }
  293. else
  294. {
  295. @this[key] = updateValue;
  296. }
  297. return @this[key];
  298. }
  299. /// <summary>
  300. /// 添加或更新键值对
  301. /// </summary>
  302. /// <typeparam name="TKey"></typeparam>
  303. /// <typeparam name="TValue"></typeparam>
  304. /// <param name="this"></param>
  305. /// <param name="that">另一个字典集</param>
  306. /// <param name="updateValueFactory">更新时的操作</param>
  307. /// <returns></returns>
  308. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  309. {
  310. foreach (var item in that)
  311. {
  312. AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
  313. }
  314. }
  315. /// <summary>
  316. /// 添加或更新键值对
  317. /// </summary>
  318. /// <typeparam name="TKey"></typeparam>
  319. /// <typeparam name="TValue"></typeparam>
  320. /// <param name="this"></param>
  321. /// <param name="that">另一个字典集</param>
  322. /// <param name="updateValueFactory">更新时的操作</param>
  323. /// <returns></returns>
  324. public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  325. {
  326. foreach (var item in that)
  327. {
  328. AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
  329. }
  330. }
  331. /// <summary>
  332. /// 添加或更新键值对
  333. /// </summary>
  334. /// <typeparam name="TKey"></typeparam>
  335. /// <typeparam name="TValue"></typeparam>
  336. /// <param name="this"></param>
  337. /// <param name="that">另一个字典集</param>
  338. /// <param name="updateValueFactory">更新时的操作</param>
  339. /// <returns></returns>
  340. public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  341. {
  342. foreach (var item in that)
  343. {
  344. AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
  345. }
  346. }
  347. /// <summary>
  348. /// 添加或更新键值对
  349. /// </summary>
  350. /// <typeparam name="TKey"></typeparam>
  351. /// <typeparam name="TValue"></typeparam>
  352. /// <param name="this"></param>
  353. /// <param name="that">另一个字典集</param>
  354. /// <param name="updateValueFactory">更新时的操作</param>
  355. /// <returns></returns>
  356. public static Task AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  357. {
  358. return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
  359. }
  360. /// <summary>
  361. /// 添加或更新键值对
  362. /// </summary>
  363. /// <typeparam name="TKey"></typeparam>
  364. /// <typeparam name="TValue"></typeparam>
  365. /// <param name="this"></param>
  366. /// <param name="that">另一个字典集</param>
  367. /// <param name="updateValueFactory">更新时的操作</param>
  368. /// <returns></returns>
  369. public static Task AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  370. {
  371. return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
  372. }
  373. /// <summary>
  374. /// 添加或更新键值对
  375. /// </summary>
  376. /// <typeparam name="TKey"></typeparam>
  377. /// <typeparam name="TValue"></typeparam>
  378. /// <param name="this"></param>
  379. /// <param name="that">另一个字典集</param>
  380. /// <param name="updateValueFactory">更新时的操作</param>
  381. /// <returns></returns>
  382. public static Task AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  383. {
  384. return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
  385. }
  386. /// <summary>
  387. /// 添加或更新键值对
  388. /// </summary>
  389. /// <typeparam name="TKey"></typeparam>
  390. /// <typeparam name="TValue"></typeparam>
  391. /// <param name="this"></param>
  392. /// <param name="that">另一个字典集</param>
  393. /// <param name="updateValueFactory">更新时的操作</param>
  394. /// <returns></returns>
  395. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  396. {
  397. foreach (var item in @this)
  398. {
  399. AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
  400. }
  401. }
  402. /// <summary>
  403. /// 添加或更新键值对
  404. /// </summary>
  405. /// <typeparam name="TKey"></typeparam>
  406. /// <typeparam name="TValue"></typeparam>
  407. /// <param name="this"></param>
  408. /// <param name="that">另一个字典集</param>
  409. /// <param name="updateValueFactory">更新时的操作</param>
  410. /// <returns></returns>
  411. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  412. {
  413. foreach (var item in @this)
  414. {
  415. AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
  416. }
  417. }
  418. /// <summary>
  419. /// 添加或更新键值对
  420. /// </summary>
  421. /// <typeparam name="TKey"></typeparam>
  422. /// <typeparam name="TValue"></typeparam>
  423. /// <param name="this"></param>
  424. /// <param name="that">另一个字典集</param>
  425. /// <param name="updateValueFactory">更新时的操作</param>
  426. /// <returns></returns>
  427. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  428. {
  429. foreach (var item in @this)
  430. {
  431. AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
  432. }
  433. }
  434. /// <summary>
  435. /// 添加或更新键值对
  436. /// </summary>
  437. /// <typeparam name="TKey"></typeparam>
  438. /// <typeparam name="TValue"></typeparam>
  439. /// <param name="this"></param>
  440. /// <param name="that">另一个字典集</param>
  441. /// <param name="updateValueFactory">更新时的操作</param>
  442. /// <returns></returns>
  443. public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  444. {
  445. return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
  446. }
  447. /// <summary>
  448. /// 添加或更新键值对
  449. /// </summary>
  450. /// <typeparam name="TKey"></typeparam>
  451. /// <typeparam name="TValue"></typeparam>
  452. /// <param name="this"></param>
  453. /// <param name="that">另一个字典集</param>
  454. /// <param name="updateValueFactory">更新时的操作</param>
  455. /// <returns></returns>
  456. public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  457. {
  458. return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
  459. }
  460. /// <summary>
  461. /// 添加或更新键值对
  462. /// </summary>
  463. /// <typeparam name="TKey"></typeparam>
  464. /// <typeparam name="TValue"></typeparam>
  465. /// <param name="this"></param>
  466. /// <param name="that">另一个字典集</param>
  467. /// <param name="updateValueFactory">更新时的操作</param>
  468. /// <returns></returns>
  469. public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  470. {
  471. return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
  472. }
  473. /// <summary>
  474. /// 添加或更新键值对
  475. /// </summary>
  476. /// <typeparam name="TKey"></typeparam>
  477. /// <typeparam name="TValue"></typeparam>
  478. /// <param name="this"></param>
  479. /// <param name="key">键</param>
  480. /// <param name="addValueFactory">添加时的操作</param>
  481. /// <param name="updateValueFactory">更新时的操作</param>
  482. /// <returns></returns>
  483. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  484. {
  485. if ([email protected](key))
  486. {
  487. @this.Add(key, addValueFactory(key));
  488. }
  489. else
  490. {
  491. @this[key] = updateValueFactory(key, @this[key]);
  492. }
  493. return @this[key];
  494. }
  495. /// <summary>
  496. /// 添加或更新键值对
  497. /// </summary>
  498. /// <typeparam name="TKey"></typeparam>
  499. /// <typeparam name="TValue"></typeparam>
  500. /// <param name="this"></param>
  501. /// <param name="key">键</param>
  502. /// <param name="addValueFactory">添加时的操作</param>
  503. /// <param name="updateValueFactory">更新时的操作</param>
  504. /// <returns></returns>
  505. public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  506. {
  507. if ([email protected](key))
  508. {
  509. @this.Add(key, addValueFactory(key));
  510. }
  511. else
  512. {
  513. @this[key] = updateValueFactory(key, @this[key]);
  514. }
  515. return @this[key];
  516. }
  517. /// <summary>
  518. /// 添加或更新键值对
  519. /// </summary>
  520. /// <typeparam name="TKey"></typeparam>
  521. /// <typeparam name="TValue"></typeparam>
  522. /// <param name="this"></param>
  523. /// <param name="key">键</param>
  524. /// <param name="addValueFactory">添加时的操作</param>
  525. /// <param name="updateValueFactory">更新时的操作</param>
  526. /// <returns></returns>
  527. public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  528. {
  529. if ([email protected](key))
  530. {
  531. @this.TryAdd(key, addValueFactory(key));
  532. }
  533. else
  534. {
  535. @this[key] = updateValueFactory(key, @this[key]);
  536. }
  537. return @this[key];
  538. }
  539. /// <summary>
  540. /// 添加或更新键值对
  541. /// </summary>
  542. /// <typeparam name="TKey"></typeparam>
  543. /// <typeparam name="TValue"></typeparam>
  544. /// <param name="this"></param>
  545. /// <param name="key">键</param>
  546. /// <param name="addValueFactory">添加时的操作</param>
  547. /// <param name="updateValueFactory">更新时的操作</param>
  548. /// <returns></returns>
  549. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  550. {
  551. if ([email protected](key))
  552. {
  553. @this.Add(key, await addValueFactory(key));
  554. }
  555. else
  556. {
  557. @this[key] = await updateValueFactory(key, @this[key]);
  558. }
  559. return @this[key];
  560. }
  561. /// <summary>
  562. /// 添加或更新键值对
  563. /// </summary>
  564. /// <typeparam name="TKey"></typeparam>
  565. /// <typeparam name="TValue"></typeparam>
  566. /// <param name="this"></param>
  567. /// <param name="key">键</param>
  568. /// <param name="addValueFactory">添加时的操作</param>
  569. /// <param name="updateValueFactory">更新时的操作</param>
  570. /// <returns></returns>
  571. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  572. {
  573. if ([email protected](key))
  574. {
  575. @this.Add(key, await addValueFactory(key));
  576. }
  577. else
  578. {
  579. @this[key] = await updateValueFactory(key, @this[key]);
  580. }
  581. return @this[key];
  582. }
  583. /// <summary>
  584. /// 添加或更新键值对
  585. /// </summary>
  586. /// <typeparam name="TKey"></typeparam>
  587. /// <typeparam name="TValue"></typeparam>
  588. /// <param name="this"></param>
  589. /// <param name="key">键</param>
  590. /// <param name="addValueFactory">添加时的操作</param>
  591. /// <param name="updateValueFactory">更新时的操作</param>
  592. /// <returns></returns>
  593. public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
  594. {
  595. if ([email protected](key))
  596. {
  597. @this.TryAdd(key, await addValueFactory(key));
  598. }
  599. else
  600. {
  601. @this[key] = await updateValueFactory(key, @this[key]);
  602. }
  603. return @this[key];
  604. }
  605. /// <summary>
  606. /// 获取或添加
  607. /// </summary>
  608. /// <typeparam name="TKey"></typeparam>
  609. /// <typeparam name="TValue"></typeparam>
  610. /// <param name="this"></param>
  611. /// <param name="key"></param>
  612. /// <param name="addValueFactory"></param>
  613. /// <returns></returns>
  614. public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TValue> addValueFactory)
  615. {
  616. if ([email protected](key))
  617. {
  618. @this[key] = addValueFactory();
  619. }
  620. return @this[key];
  621. }
  622. /// <summary>
  623. /// 获取或添加
  624. /// </summary>
  625. /// <typeparam name="TKey"></typeparam>
  626. /// <typeparam name="TValue"></typeparam>
  627. /// <param name="this"></param>
  628. /// <param name="key"></param>
  629. /// <param name="addValueFactory"></param>
  630. /// <returns></returns>
  631. public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
  632. {
  633. if ([email protected](key))
  634. {
  635. @this[key] = await addValueFactory();
  636. }
  637. return @this[key];
  638. }
  639. /// <summary>
  640. /// 获取或添加
  641. /// </summary>
  642. /// <typeparam name="TKey"></typeparam>
  643. /// <typeparam name="TValue"></typeparam>
  644. /// <param name="this"></param>
  645. /// <param name="key"></param>
  646. /// <param name="addValue"></param>
  647. /// <returns></returns>
  648. public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
  649. {
  650. if ([email protected](key))
  651. {
  652. @this[key] = addValue;
  653. return addValue;
  654. }
  655. return @this[key];
  656. }
  657. /// <summary>
  658. /// 遍历IEnumerable
  659. /// </summary>
  660. /// <param name="dic"></param>
  661. /// <param name="action">回调方法</param>
  662. public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dic, Action<TKey, TValue> action)
  663. {
  664. foreach (var item in dic)
  665. {
  666. action(item.Key, item.Value);
  667. }
  668. }
  669. /// <summary>
  670. /// 遍历IDictionary
  671. /// </summary>
  672. /// <param name="dic"></param>
  673. /// <param name="action">回调方法</param>
  674. public static Task ForEachAsync<TKey, TValue>(this IDictionary<TKey, TValue> dic, Func<TKey, TValue, Task> action)
  675. {
  676. return dic.ForeachAsync(x => action(x.Key, x.Value));
  677. }
  678. /// <summary>
  679. /// 安全的转换成字典集
  680. /// </summary>
  681. /// <typeparam name="TSource"></typeparam>
  682. /// <typeparam name="TKey"></typeparam>
  683. /// <param name="source"></param>
  684. /// <param name="keySelector">键选择器</param>
  685. /// <returns></returns>
  686. public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  687. {
  688. var dic = new NullableDictionary<TKey, TSource>(source.Count());
  689. foreach (var item in source)
  690. {
  691. dic[keySelector(item)] = item;
  692. }
  693. return dic;
  694. }
  695. /// <summary>
  696. /// 安全的转换成字典集
  697. /// </summary>
  698. /// <typeparam name="TSource"></typeparam>
  699. /// <typeparam name="TKey"></typeparam>
  700. /// <param name="source"></param>
  701. /// <param name="keySelector">键选择器</param>
  702. /// <param name="defaultValue">键未找到时的默认值</param>
  703. /// <returns></returns>
  704. public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
  705. {
  706. var dic = new NullableDictionary<TKey, TSource>(source.Count()) { FallbackValue = defaultValue };
  707. foreach (var item in source)
  708. {
  709. dic[keySelector(item)] = item;
  710. }
  711. return dic;
  712. }
  713. /// <summary>
  714. /// 安全的转换成字典集
  715. /// </summary>
  716. /// <typeparam name="TSource"></typeparam>
  717. /// <typeparam name="TKey"></typeparam>
  718. /// <typeparam name="TElement"></typeparam>
  719. /// <param name="source"></param>
  720. /// <param name="keySelector">键选择器</param>
  721. /// <param name="elementSelector">值选择器</param>
  722. /// <returns></returns>
  723. public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  724. {
  725. var dic = new NullableDictionary<TKey, TElement>(source.Count());
  726. foreach (var item in source)
  727. {
  728. dic[keySelector(item)] = elementSelector(item);
  729. }
  730. return dic;
  731. }
  732. /// <summary>
  733. /// 安全的转换成字典集
  734. /// </summary>
  735. /// <typeparam name="TSource"></typeparam>
  736. /// <typeparam name="TKey"></typeparam>
  737. /// <typeparam name="TElement"></typeparam>
  738. /// <param name="source"></param>
  739. /// <param name="keySelector">键选择器</param>
  740. /// <param name="elementSelector">值选择器</param>
  741. /// <param name="defaultValue">键未找到时的默认值</param>
  742. /// <returns></returns>
  743. public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
  744. {
  745. var dic = new NullableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
  746. foreach (var item in source)
  747. {
  748. dic[keySelector(item)] = elementSelector(item);
  749. }
  750. return dic;
  751. }
  752. /// <summary>
  753. /// 安全的转换成字典集
  754. /// </summary>
  755. /// <typeparam name="TSource"></typeparam>
  756. /// <typeparam name="TKey"></typeparam>
  757. /// <typeparam name="TElement"></typeparam>
  758. /// <param name="source"></param>
  759. /// <param name="keySelector">键选择器</param>
  760. /// <param name="elementSelector">值选择器</param>
  761. /// <returns></returns>
  762. public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
  763. {
  764. var dic = new NullableDictionary<TKey, TElement>(source.Count());
  765. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  766. return dic;
  767. }
  768. /// <summary>
  769. /// 安全的转换成字典集
  770. /// </summary>
  771. /// <typeparam name="TSource"></typeparam>
  772. /// <typeparam name="TKey"></typeparam>
  773. /// <typeparam name="TElement"></typeparam>
  774. /// <param name="source"></param>
  775. /// <param name="keySelector">键选择器</param>
  776. /// <param name="elementSelector">值选择器</param>
  777. /// <param name="defaultValue">键未找到时的默认值</param>
  778. /// <returns></returns>
  779. public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
  780. {
  781. var dic = new NullableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
  782. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  783. return dic;
  784. }
  785. /// <summary>
  786. /// 安全的转换成字典集
  787. /// </summary>
  788. /// <typeparam name="TSource"></typeparam>
  789. /// <typeparam name="TKey"></typeparam>
  790. /// <param name="source"></param>
  791. /// <param name="keySelector">键选择器</param>
  792. /// <returns></returns>
  793. public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
  794. {
  795. var dic = new DisposableDictionary<TKey, TSource>(source.Count());
  796. foreach (var item in source)
  797. {
  798. dic[keySelector(item)] = item;
  799. }
  800. return dic;
  801. }
  802. /// <summary>
  803. /// 安全的转换成字典集
  804. /// </summary>
  805. /// <typeparam name="TSource"></typeparam>
  806. /// <typeparam name="TKey"></typeparam>
  807. /// <param name="source"></param>
  808. /// <param name="keySelector">键选择器</param>
  809. /// <param name="defaultValue">键未找到时的默认值</param>
  810. /// <returns></returns>
  811. public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
  812. {
  813. var dic = new DisposableDictionary<TKey, TSource>(source.Count()) { FallbackValue = defaultValue };
  814. foreach (var item in source)
  815. {
  816. dic[keySelector(item)] = item;
  817. }
  818. return dic;
  819. }
  820. /// <summary>
  821. /// 安全的转换成字典集
  822. /// </summary>
  823. /// <typeparam name="TSource"></typeparam>
  824. /// <typeparam name="TKey"></typeparam>
  825. /// <typeparam name="TElement"></typeparam>
  826. /// <param name="source"></param>
  827. /// <param name="keySelector">键选择器</param>
  828. /// <param name="elementSelector">值选择器</param>
  829. /// <returns></returns>
  830. public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
  831. {
  832. var dic = new DisposableDictionary<TKey, TElement>(source.Count());
  833. foreach (var item in source)
  834. {
  835. dic[keySelector(item)] = elementSelector(item);
  836. }
  837. return dic;
  838. }
  839. /// <summary>
  840. /// 安全的转换成字典集
  841. /// </summary>
  842. /// <typeparam name="TSource"></typeparam>
  843. /// <typeparam name="TKey"></typeparam>
  844. /// <typeparam name="TElement"></typeparam>
  845. /// <param name="source"></param>
  846. /// <param name="keySelector">键选择器</param>
  847. /// <param name="elementSelector">值选择器</param>
  848. /// <param name="defaultValue">键未找到时的默认值</param>
  849. /// <returns></returns>
  850. public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
  851. {
  852. var dic = new DisposableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
  853. foreach (var item in source)
  854. {
  855. dic[keySelector(item)] = elementSelector(item);
  856. }
  857. return dic;
  858. }
  859. /// <summary>
  860. /// 安全的转换成字典集
  861. /// </summary>
  862. /// <typeparam name="TSource"></typeparam>
  863. /// <typeparam name="TKey"></typeparam>
  864. /// <typeparam name="TElement"></typeparam>
  865. /// <param name="source"></param>
  866. /// <param name="keySelector">键选择器</param>
  867. /// <param name="elementSelector">值选择器</param>
  868. /// <returns></returns>
  869. public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
  870. {
  871. var dic = new DisposableDictionary<TKey, TElement>(source.Count());
  872. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  873. return dic;
  874. }
  875. /// <summary>
  876. /// 安全的转换成字典集
  877. /// </summary>
  878. /// <typeparam name="TSource"></typeparam>
  879. /// <typeparam name="TKey"></typeparam>
  880. /// <typeparam name="TElement"></typeparam>
  881. /// <param name="source"></param>
  882. /// <param name="keySelector">键选择器</param>
  883. /// <param name="elementSelector">值选择器</param>
  884. /// <param name="defaultValue">键未找到时的默认值</param>
  885. /// <returns></returns>
  886. public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
  887. {
  888. var dic = new DisposableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
  889. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  890. return dic;
  891. }
  892. /// <summary>
  893. /// 安全的转换成字典集
  894. /// </summary>
  895. /// <typeparam name="TSource"></typeparam>
  896. /// <typeparam name="TKey"></typeparam>
  897. /// <param name="source"></param>
  898. /// <param name="keySelector">键选择器</param>
  899. /// <returns></returns>
  900. public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  901. {
  902. var dic = new NullableConcurrentDictionary<TKey, TSource>();
  903. foreach (var item in source)
  904. {
  905. dic[keySelector(item)] = item;
  906. }
  907. return dic;
  908. }
  909. /// <summary>
  910. /// 安全的转换成字典集
  911. /// </summary>
  912. /// <typeparam name="TSource"></typeparam>
  913. /// <typeparam name="TKey"></typeparam>
  914. /// <param name="source"></param>
  915. /// <param name="keySelector">键选择器</param>
  916. /// <param name="defaultValue">键未找到时的默认值</param>
  917. /// <returns></returns>
  918. public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
  919. {
  920. var dic = new NullableConcurrentDictionary<TKey, TSource>() { FallbackValue = defaultValue };
  921. foreach (var item in source)
  922. {
  923. dic[keySelector(item)] = item;
  924. }
  925. return dic;
  926. }
  927. /// <summary>
  928. /// 安全的转换成字典集
  929. /// </summary>
  930. /// <typeparam name="TSource"></typeparam>
  931. /// <typeparam name="TKey"></typeparam>
  932. /// <typeparam name="TElement"></typeparam>
  933. /// <param name="source"></param>
  934. /// <param name="keySelector">键选择器</param>
  935. /// <param name="elementSelector">值选择器</param>
  936. /// <returns></returns>
  937. public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  938. {
  939. var dic = new NullableConcurrentDictionary<TKey, TElement>();
  940. foreach (var item in source)
  941. {
  942. dic[keySelector(item)] = elementSelector(item);
  943. }
  944. return dic;
  945. }
  946. /// <summary>
  947. /// 安全的转换成字典集
  948. /// </summary>
  949. /// <typeparam name="TSource"></typeparam>
  950. /// <typeparam name="TKey"></typeparam>
  951. /// <typeparam name="TElement"></typeparam>
  952. /// <param name="source"></param>
  953. /// <param name="keySelector">键选择器</param>
  954. /// <param name="elementSelector">值选择器</param>
  955. /// <returns></returns>
  956. public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
  957. {
  958. var dic = new NullableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
  959. foreach (var item in source)
  960. {
  961. dic[keySelector(item)] = elementSelector(item);
  962. }
  963. return dic;
  964. }
  965. /// <summary>
  966. /// 安全的转换成字典集
  967. /// </summary>
  968. /// <typeparam name="TSource"></typeparam>
  969. /// <typeparam name="TKey"></typeparam>
  970. /// <typeparam name="TElement"></typeparam>
  971. /// <param name="source"></param>
  972. /// <param name="keySelector">键选择器</param>
  973. /// <param name="elementSelector">值选择器</param>
  974. /// <returns></returns>
  975. public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
  976. {
  977. var dic = new ConcurrentDictionary<TKey, TElement>();
  978. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  979. return dic;
  980. }
  981. /// <summary>
  982. /// 安全的转换成字典集
  983. /// </summary>
  984. /// <typeparam name="TSource"></typeparam>
  985. /// <typeparam name="TKey"></typeparam>
  986. /// <typeparam name="TElement"></typeparam>
  987. /// <param name="source"></param>
  988. /// <param name="keySelector">键选择器</param>
  989. /// <param name="elementSelector">值选择器</param>
  990. /// <param name="defaultValue">键未找到时的默认值</param>
  991. /// <returns></returns>
  992. public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
  993. {
  994. var dic = new NullableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
  995. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  996. return dic;
  997. }
  998. /// <summary>
  999. /// 安全的转换成字典集
  1000. /// </summary>
  1001. /// <typeparam name="TSource"></typeparam>
  1002. /// <typeparam name="TKey"></typeparam>
  1003. /// <param name="source"></param>
  1004. /// <param name="keySelector">键选择器</param>
  1005. /// <returns></returns>
  1006. public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
  1007. {
  1008. var dic = new DisposableConcurrentDictionary<TKey, TSource>();
  1009. foreach (var item in source)
  1010. {
  1011. dic[keySelector(item)] = item;
  1012. }
  1013. return dic;
  1014. }
  1015. /// <summary>
  1016. /// 安全的转换成字典集
  1017. /// </summary>
  1018. /// <typeparam name="TSource"></typeparam>
  1019. /// <typeparam name="TKey"></typeparam>
  1020. /// <param name="source"></param>
  1021. /// <param name="keySelector">键选择器</param>
  1022. /// <param name="defaultValue">键未找到时的默认值</param>
  1023. /// <returns></returns>
  1024. public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
  1025. {
  1026. var dic = new DisposableConcurrentDictionary<TKey, TSource>() { FallbackValue = defaultValue };
  1027. foreach (var item in source)
  1028. {
  1029. dic[keySelector(item)] = item;
  1030. }
  1031. return dic;
  1032. }
  1033. /// <summary>
  1034. /// 安全的转换成字典集
  1035. /// </summary>
  1036. /// <typeparam name="TSource"></typeparam>
  1037. /// <typeparam name="TKey"></typeparam>
  1038. /// <typeparam name="TElement"></typeparam>
  1039. /// <param name="source"></param>
  1040. /// <param name="keySelector">键选择器</param>
  1041. /// <param name="elementSelector">值选择器</param>
  1042. /// <returns></returns>
  1043. public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
  1044. {
  1045. var dic = new DisposableConcurrentDictionary<TKey, TElement>();
  1046. foreach (var item in source)
  1047. {
  1048. dic[keySelector(item)] = elementSelector(item);
  1049. }
  1050. return dic;
  1051. }
  1052. /// <summary>
  1053. /// 安全的转换成字典集
  1054. /// </summary>
  1055. /// <typeparam name="TSource"></typeparam>
  1056. /// <typeparam name="TKey"></typeparam>
  1057. /// <typeparam name="TElement"></typeparam>
  1058. /// <param name="source"></param>
  1059. /// <param name="keySelector">键选择器</param>
  1060. /// <param name="elementSelector">值选择器</param>
  1061. /// <returns></returns>
  1062. public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
  1063. {
  1064. var dic = new DisposableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
  1065. foreach (var item in source)
  1066. {
  1067. dic[keySelector(item)] = elementSelector(item);
  1068. }
  1069. return dic;
  1070. }
  1071. /// <summary>
  1072. /// 安全的转换成字典集
  1073. /// </summary>
  1074. /// <typeparam name="TSource"></typeparam>
  1075. /// <typeparam name="TKey"></typeparam>
  1076. /// <typeparam name="TElement"></typeparam>
  1077. /// <param name="source"></param>
  1078. /// <param name="keySelector">键选择器</param>
  1079. /// <param name="elementSelector">值选择器</param>
  1080. /// <returns></returns>
  1081. public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
  1082. {
  1083. var dic = new DisposableConcurrentDictionary<TKey, TElement>();
  1084. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  1085. return dic;
  1086. }
  1087. /// <summary>
  1088. /// 安全的转换成字典集
  1089. /// </summary>
  1090. /// <typeparam name="TSource"></typeparam>
  1091. /// <typeparam name="TKey"></typeparam>
  1092. /// <typeparam name="TElement"></typeparam>
  1093. /// <param name="source"></param>
  1094. /// <param name="keySelector">键选择器</param>
  1095. /// <param name="elementSelector">值选择器</param>
  1096. /// <param name="defaultValue">键未找到时的默认值</param>
  1097. /// <returns></returns>
  1098. public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
  1099. {
  1100. var dic = new DisposableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
  1101. await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
  1102. return dic;
  1103. }
  1104. /// <summary>
  1105. /// 转换成并发字典集合
  1106. /// </summary>
  1107. /// <typeparam name="TKey"></typeparam>
  1108. /// <typeparam name="TValue"></typeparam>
  1109. /// <param name="dic"></param>
  1110. /// <returns></returns>
  1111. public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic) => dic;
  1112. /// <summary>
  1113. /// 转换成并发字典集合
  1114. /// </summary>
  1115. /// <typeparam name="TKey"></typeparam>
  1116. /// <typeparam name="TValue"></typeparam>
  1117. /// <param name="dic"></param>
  1118. /// <param name="defaultValue">键未找到时的默认值</param>
  1119. /// <returns></returns>
  1120. public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue defaultValue)
  1121. {
  1122. var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>() { FallbackValue = defaultValue };
  1123. foreach (var p in dic)
  1124. {
  1125. nullableDictionary[p.Key] = p.Value;
  1126. }
  1127. return nullableDictionary;
  1128. }
  1129. /// <summary>
  1130. /// 转换成普通字典集合
  1131. /// </summary>
  1132. /// <typeparam name="TKey"></typeparam>
  1133. /// <typeparam name="TValue"></typeparam>
  1134. /// <param name="dic"></param>
  1135. /// <returns></returns>
  1136. public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic) => dic;
  1137. /// <summary>
  1138. /// 转换成普通字典集合
  1139. /// </summary>
  1140. /// <typeparam name="TKey"></typeparam>
  1141. /// <typeparam name="TValue"></typeparam>
  1142. /// <param name="dic"></param>
  1143. /// <param name="defaultValue">键未找到时的默认值</param>
  1144. /// <returns></returns>
  1145. public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic, TValue defaultValue)
  1146. {
  1147. var nullableDictionary = new NullableDictionary<TKey, TValue>() { FallbackValue = defaultValue };
  1148. foreach (var p in dic)
  1149. {
  1150. nullableDictionary[p.Key] = p.Value;
  1151. }
  1152. return nullableDictionary;
  1153. }
  1154. }
  1155. }