ObservableDictionary.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace IFramework
  6. {
  7. /// <summary>
  8. /// 可观测Dictionary
  9. /// </summary>
  10. /// <typeparam name="TKey">TKey</typeparam>
  11. /// <typeparam name="TValue">TValue</typeparam>
  12. public class ObservableDictionary<TKey, TValue> : Unit, IDictionary<TKey, TValue>, IDictionary
  13. {
  14. #region 事件与变量定义
  15. private Action<KeyValuePair<TKey, TValue>> onKeyValuePairAdded;
  16. private Action<KeyValuePair<TKey, TValue>, KeyValuePair<TKey, TValue>> onKeyValuePairReplaced;
  17. private Action<KeyValuePair<TKey, TValue>> onKeyValuePairRemoved;
  18. private Action onClear;
  19. private Action<KeyValuePair<TKey, TValue>[]> onKeyValuePairRangeAdded;
  20. private Lazy<Dictionary<TKey, TValue>> _dictionary = new Lazy<Dictionary<TKey, TValue>>(() => { return new Dictionary<TKey, TValue>(); });
  21. private Dictionary<TKey, TValue> dictionary { get { return _dictionary.Value; } }
  22. #endregion
  23. #region 注册与移除监听方法
  24. /// <summary>
  25. /// 注册方法 添加键值对
  26. /// </summary>
  27. /// <param name="action">
  28. /// action arg1 : the Added KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  29. /// 参数1为添加的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  30. /// </param>
  31. public void SubscribeAddKeyValuePair(Action<KeyValuePair<TKey, TValue>> action)
  32. {
  33. onKeyValuePairAdded += action;
  34. }
  35. /// <summary>
  36. /// 移除方法 添加键值对
  37. /// </summary>
  38. /// <param name="action">
  39. /// action arg1 : the Added KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  40. /// 参数1为添加的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  41. /// </param>
  42. public void UnSubscribeAddKeyValuePair(Action<KeyValuePair<TKey, TValue>> action)
  43. {
  44. onKeyValuePairAdded -= action;
  45. }
  46. /// <summary>
  47. /// 注册方法 替换键值对
  48. /// </summary>
  49. /// <param name = "action" >
  50. /// action arg1 : the old KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /> which has been replaced<br/>
  51. /// 参数1为被替换的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" /><br/><br/>
  52. /// action arg2 : the new KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /> which replaced the old one<br/>
  53. /// 参数1为新的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" /><br/>
  54. /// </param>
  55. public void SubscribeReplaceKeyValuePair(Action<KeyValuePair<TKey, TValue>, KeyValuePair<TKey, TValue>> action)
  56. {
  57. onKeyValuePairReplaced += action;
  58. }
  59. /// <summary>
  60. /// 移除方法 替换键值对
  61. /// </summary>
  62. /// <param name = "action" >
  63. /// action arg1 : the old KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /> which has been replaced<br/>
  64. /// 参数1为被替换的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" /><br/><br/>
  65. /// action arg2 : the new KeyValuePair of <typeparamref name="TKey" /> and <typeparamref name="TValue" /> which replaced the old one<br/>
  66. /// 参数1为新的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" /><br/>
  67. /// </param>
  68. public void UnSubscribeReplaceKeyValuePair(Action<KeyValuePair<TKey, TValue>, KeyValuePair<TKey, TValue>> action)
  69. {
  70. onKeyValuePairReplaced -= action;
  71. }
  72. /// <summary>
  73. /// 注册方法 移除键值对
  74. /// </summary>
  75. /// <param name="action">
  76. /// action arg1 : the removed KeyValuePair with <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  77. /// 参数1为移除的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  78. /// </param>
  79. public void SubscribeRemoveKeyValuePair(Action<KeyValuePair<TKey, TValue>> action)
  80. {
  81. onKeyValuePairRemoved += action;
  82. }
  83. /// <summary>
  84. /// 移除方法 移除键值对
  85. /// </summary>
  86. /// <param name="action">
  87. /// action arg1 : the removed KeyValuePair with <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  88. /// 参数1为移除的键值对对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  89. /// </param>
  90. public void UnSubscribeRemoveKeyValuePair(Action<KeyValuePair<TKey, TValue>> action)
  91. {
  92. onKeyValuePairRemoved -= action;
  93. }
  94. /// <summary>
  95. /// 注册方法 清除所有键值对
  96. /// </summary>
  97. /// <param name="action">none arg Action</param>
  98. public void SubscribeClear(Action action)
  99. {
  100. onClear += action;
  101. }
  102. /// <summary>
  103. /// 移除方法 清除所有键值对
  104. /// </summary>
  105. /// <param name="action">none arg Action</param>
  106. public void UnSbscribeClear(Action action)
  107. {
  108. onClear -= action;
  109. }
  110. /// <summary>
  111. /// 注册方法 添加多个键值对
  112. /// </summary>
  113. /// <param name="action">
  114. /// action arg1 : the Added array of KeyValuePair with <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  115. /// 参数1为添加的键值对数组对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  116. /// </param>
  117. public void SubscribeAddRange(Action<KeyValuePair<TKey, TValue>[]> action)
  118. {
  119. onKeyValuePairRangeAdded += action;
  120. }
  121. /// <summary>
  122. /// 移除方法 添加添加多个键值对
  123. /// </summary>
  124. /// <param name="action">
  125. /// action arg1 : the Added array of KeyValuePair with <typeparamref name="TKey" /> and <typeparamref name="TValue" /><br/>
  126. /// 参数1为添加的键值对数组对象,键的类型为<typeparamref name="TKey" />,值的类型为<typeparamref name="TValue" />
  127. /// </param>
  128. public void UnSubscribeAddRange(Action<KeyValuePair<TKey, TValue>[]> action)
  129. {
  130. onKeyValuePairRangeAdded -= action;
  131. }
  132. #endregion
  133. #region 接口实现
  134. /// <summary>
  135. /// 索引器
  136. /// </summary>
  137. /// <param name="key">键</param>
  138. /// <returns>值</returns>
  139. public TValue this[TKey key]
  140. {
  141. get
  142. {
  143. if (!dictionary.ContainsKey(key))
  144. return default(TValue);
  145. return dictionary[key];
  146. }
  147. set
  148. {
  149. Insert(key, value, false);
  150. }
  151. }
  152. /// <summary>
  153. /// 键集合
  154. /// </summary>
  155. public ICollection<TKey> Keys
  156. {
  157. get { return dictionary.Keys; }
  158. }
  159. /// <summary>
  160. /// 值集合
  161. /// </summary>
  162. public ICollection<TValue> Values
  163. {
  164. get { return dictionary.Values; }
  165. }
  166. /// <summary>
  167. /// 数量
  168. /// </summary>
  169. public int Count
  170. {
  171. get { return dictionary.Count; }
  172. }
  173. /// <summary>
  174. /// 是否只读
  175. /// </summary>
  176. public bool IsReadOnly
  177. {
  178. get { return ((IDictionary)this.dictionary).IsReadOnly; }
  179. }
  180. /// <summary>
  181. /// 添加键值对
  182. /// </summary>
  183. /// <param name="key">键</param>
  184. /// <param name="value">值</param>
  185. public void Add(TKey key, TValue value)
  186. {
  187. Insert(key, value, true);
  188. }
  189. /// <summary>
  190. /// 添加键值对
  191. /// </summary>
  192. /// <param name="pair">键值对</param>
  193. public void Add(KeyValuePair<TKey, TValue> pair)
  194. {
  195. Insert(pair.Key, pair.Value, true);
  196. }
  197. /// <summary>
  198. /// 清除所有键值对
  199. /// </summary>
  200. public void Clear()
  201. {
  202. if (dictionary.Count > 0)
  203. {
  204. dictionary.Clear();
  205. onClear?.Invoke();
  206. }
  207. }
  208. /// <summary>
  209. /// 是否存在键值对
  210. /// </summary>
  211. /// <param name="pair">键值对</param>
  212. /// <returns></returns>
  213. public bool Contains(KeyValuePair<TKey, TValue> pair)
  214. {
  215. return dictionary.Contains(pair);
  216. }
  217. /// <summary>
  218. /// 是否存在键
  219. /// </summary>
  220. /// <param name="key">键</param>
  221. /// <returns>存在情况</returns>
  222. public bool ContainsKey(TKey key)
  223. {
  224. return dictionary.ContainsKey(key);
  225. }
  226. /// <summary>
  227. /// 将字典里的键值对拷贝到指定数组中
  228. /// </summary>
  229. /// <param name="array">接收的数组</param>
  230. /// <param name="arrayIndex">开始保存在数组的索引</param>
  231. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  232. {
  233. ((IDictionary)this.dictionary).CopyTo(array, arrayIndex);
  234. }
  235. /// <summary>
  236. /// 获取迭代器
  237. /// </summary>
  238. /// <returns>迭代器</returns>
  239. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  240. {
  241. return dictionary.GetEnumerator();
  242. }
  243. /// <summary>
  244. /// 删除键值对
  245. /// </summary>
  246. /// <param name="key">键</param>
  247. /// <returns>是否删除成功</returns>
  248. public bool Remove(TKey key)
  249. {
  250. if (key == null)
  251. throw new ArgumentNullException("key is null");
  252. dictionary.TryGetValue(key, out TValue value);
  253. var removed = dictionary.Remove(key);
  254. if (removed)
  255. {
  256. onKeyValuePairRemoved.Invoke(new KeyValuePair<TKey, TValue>(key, value));
  257. }
  258. return removed;
  259. }
  260. /// <summary>
  261. /// 删除键值对
  262. /// </summary>
  263. /// <param name="pair">键值对</param>
  264. /// <returns>是否删除成功,如果对应的键值对不存在,返回false</returns>
  265. public bool Remove(KeyValuePair<TKey, TValue> pair)
  266. {
  267. if (Contains(pair))
  268. {
  269. return Remove(pair.Key);
  270. }
  271. return false;
  272. }
  273. /// <summary>
  274. /// 获取指定键对应的值
  275. /// </summary>
  276. /// <param name="key">键</param>
  277. /// <param name="value">保存值的对象</param>
  278. /// <returns>是否获取成功</returns>
  279. public bool TryGetValue(TKey key, out TValue value)
  280. {
  281. return dictionary.TryGetValue(key, out value);
  282. }
  283. IEnumerator IEnumerable.GetEnumerator()
  284. {
  285. return ((IEnumerable)dictionary).GetEnumerator();
  286. }
  287. object IDictionary.this[object key]
  288. {
  289. get
  290. {
  291. return ((IDictionary)this.dictionary)[key];
  292. }
  293. set
  294. {
  295. Insert((TKey)key, (TValue)value, false);
  296. }
  297. }
  298. ICollection IDictionary.Keys
  299. {
  300. get { return ((IDictionary)this.dictionary).Keys; }
  301. }
  302. ICollection IDictionary.Values
  303. {
  304. get { return ((IDictionary)this.dictionary).Values; }
  305. }
  306. IDictionaryEnumerator IDictionary.GetEnumerator()
  307. {
  308. return ((IDictionary)this.dictionary).GetEnumerator();
  309. }
  310. bool IDictionary.Contains(object key)
  311. {
  312. return ((IDictionary)this.dictionary).Contains(key);
  313. }
  314. void IDictionary.Add(object key, object value)
  315. {
  316. this.Add((TKey)key, (TValue)value);
  317. }
  318. void IDictionary.Remove(object key)
  319. {
  320. this.Remove((TKey)key);
  321. }
  322. bool IDictionary.IsFixedSize
  323. {
  324. get { return ((IDictionary)this.dictionary).IsFixedSize; }
  325. }
  326. object ICollection.SyncRoot
  327. {
  328. get { return ((IDictionary)this.dictionary).SyncRoot; }
  329. }
  330. bool ICollection.IsSynchronized
  331. {
  332. get { return ((IDictionary)this.dictionary).IsSynchronized; }
  333. }
  334. void ICollection.CopyTo(Array array, int index)
  335. {
  336. ((IDictionary)this.dictionary).CopyTo(array, index);
  337. }
  338. /// <summary>
  339. /// 对象释放时调用(继承自Unit)
  340. /// </summary>
  341. protected override void OnDispose()
  342. {
  343. dictionary.Clear();
  344. onKeyValuePairAdded -= onKeyValuePairAdded;
  345. onKeyValuePairReplaced -= onKeyValuePairReplaced;
  346. onKeyValuePairRemoved -= onKeyValuePairRemoved;
  347. }
  348. #endregion
  349. #region 额外方法实现
  350. private void Insert(TKey key, TValue value, bool isAdd)
  351. {
  352. if (key == null)
  353. throw new ArgumentNullException("key is null");
  354. if (dictionary.TryGetValue(key, out TValue oldValue))
  355. {
  356. if (isAdd)
  357. throw new ArgumentException("this key has already been added");
  358. if (Equals(oldValue, value))
  359. return;
  360. dictionary[key] = value;
  361. onKeyValuePairReplaced?.Invoke(new KeyValuePair<TKey, TValue>(key, oldValue), new KeyValuePair<TKey, TValue>(key, value));
  362. }
  363. else
  364. {
  365. dictionary[key] = value;
  366. onKeyValuePairAdded?.Invoke(new KeyValuePair<TKey, TValue>(key, value));
  367. }
  368. }
  369. /// <summary>
  370. /// 将一个字典添加到另一个字典中
  371. /// </summary>
  372. /// <param name="items">字典对象</param>
  373. public void AddRange(IDictionary<TKey, TValue> items)
  374. {
  375. if (items == null)
  376. throw new ArgumentNullException("items is null");
  377. if (items.Count > 0)
  378. {
  379. if (items.Keys.Any((k) => this.dictionary.ContainsKey(k)))
  380. throw new ArgumentException("a key or some keys in this dictionary has already been added");
  381. else
  382. {
  383. foreach (var item in items)
  384. ((IDictionary<TKey, TValue>)this.dictionary).Add(item);
  385. }
  386. onKeyValuePairRangeAdded?.Invoke(items.ToArray());
  387. }
  388. }
  389. #endregion
  390. }
  391. }