ThreadSafeDictionary.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6. using System.Runtime.Versioning;
  7. using System.Threading;
  8. namespace ILRuntime.Other
  9. {
  10. /// <summary>
  11. /// A thread safe dictionary for internal use
  12. /// </summary>
  13. /// <typeparam name="K"></typeparam>
  14. /// <typeparam name="V"></typeparam>
  15. class ThreadSafeDictionary<K, V> : IDictionary<K, V>
  16. {
  17. Dictionary<K, V> dic = new Dictionary<K, V>();
  18. public Dictionary<K,V> InnerDictionary { get { return dic; } }
  19. public V this[K key]
  20. {
  21. get
  22. {
  23. return dic[key];
  24. }
  25. set
  26. {
  27. lock(dic)
  28. dic[key] = value;
  29. }
  30. }
  31. public int Count
  32. {
  33. get
  34. {
  35. lock(dic)
  36. return dic.Count;
  37. }
  38. }
  39. public bool IsReadOnly
  40. {
  41. get
  42. {
  43. lock(dic)
  44. return IsReadOnly;
  45. }
  46. }
  47. public ICollection<K> Keys
  48. {
  49. get
  50. {
  51. throw new NotImplementedException();
  52. }
  53. }
  54. public ICollection<V> Values
  55. {
  56. get
  57. {
  58. throw new NotImplementedException();
  59. }
  60. }
  61. public void Add(KeyValuePair<K, V> item)
  62. {
  63. lock (dic)
  64. dic.Add(item.Key, item.Value);
  65. }
  66. public void Add(K key, V value)
  67. {
  68. lock(dic)
  69. dic.Add(key, value);
  70. }
  71. public void Clear()
  72. {
  73. lock(dic)
  74. dic.Clear();
  75. }
  76. public bool Contains(KeyValuePair<K, V> item)
  77. {
  78. return dic.ContainsKey(item.Key);
  79. }
  80. public bool ContainsKey(K key)
  81. {
  82. return dic.ContainsKey(key);
  83. }
  84. public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
  85. {
  86. throw new NotImplementedException();
  87. }
  88. public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
  89. {
  90. throw new NotImplementedException();
  91. }
  92. public bool Remove(KeyValuePair<K, V> item)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. public bool Remove(K key)
  97. {
  98. lock(dic)
  99. return dic.Remove(key);
  100. }
  101. public bool TryGetValue(K key, out V value)
  102. {
  103. return dic.TryGetValue(key, out value);
  104. }
  105. IEnumerator IEnumerable.GetEnumerator()
  106. {
  107. throw new NotImplementedException();
  108. }
  109. }
  110. }