ReadOnlyDictionaryInternal.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Runtime
  5. {
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. // This class is for back-compat with 4.0, where we exposed read-only dictionaries that threw
  9. // InvalidOperation if mutated. Any new usages should use the CLR's public ReadOnlyDictionary
  10. // (which throws NotSupported).
  11. [Serializable]
  12. class ReadOnlyDictionaryInternal<TKey, TValue> : IDictionary<TKey, TValue>
  13. {
  14. IDictionary<TKey, TValue> dictionary;
  15. public ReadOnlyDictionaryInternal(IDictionary<TKey, TValue> dictionary)
  16. {
  17. this.dictionary = dictionary;
  18. }
  19. public int Count
  20. {
  21. get { return this.dictionary.Count; }
  22. }
  23. public bool IsReadOnly
  24. {
  25. get { return true; }
  26. }
  27. public ICollection<TKey> Keys
  28. {
  29. get { return this.dictionary.Keys; }
  30. }
  31. public ICollection<TValue> Values
  32. {
  33. get { return this.dictionary.Values; }
  34. }
  35. public TValue this[TKey key]
  36. {
  37. get
  38. {
  39. return this.dictionary[key];
  40. }
  41. set
  42. {
  43. throw Fx.Exception.AsError(CreateReadOnlyException());
  44. }
  45. }
  46. public static IDictionary<TKey, TValue> Create(IDictionary<TKey, TValue> dictionary)
  47. {
  48. if (dictionary.IsReadOnly)
  49. {
  50. return dictionary;
  51. }
  52. else
  53. {
  54. return new ReadOnlyDictionaryInternal<TKey, TValue>(dictionary);
  55. }
  56. }
  57. Exception CreateReadOnlyException()
  58. {
  59. return new InvalidOperationException(InternalSR.DictionaryIsReadOnly);
  60. }
  61. public void Add(TKey key, TValue value)
  62. {
  63. throw Fx.Exception.AsError(CreateReadOnlyException());
  64. }
  65. public void Add(KeyValuePair<TKey, TValue> item)
  66. {
  67. throw Fx.Exception.AsError(CreateReadOnlyException());
  68. }
  69. public void Clear()
  70. {
  71. throw Fx.Exception.AsError(CreateReadOnlyException());
  72. }
  73. public bool Contains(KeyValuePair<TKey, TValue> item)
  74. {
  75. return this.dictionary.Contains(item);
  76. }
  77. public bool ContainsKey(TKey key)
  78. {
  79. return this.dictionary.ContainsKey(key);
  80. }
  81. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  82. {
  83. this.dictionary.CopyTo(array, arrayIndex);
  84. }
  85. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  86. {
  87. return this.dictionary.GetEnumerator();
  88. }
  89. IEnumerator IEnumerable.GetEnumerator()
  90. {
  91. return this.GetEnumerator();
  92. }
  93. public bool Remove(TKey key)
  94. {
  95. throw Fx.Exception.AsError(CreateReadOnlyException());
  96. }
  97. public bool Remove(KeyValuePair<TKey, TValue> item)
  98. {
  99. throw Fx.Exception.AsError(CreateReadOnlyException());
  100. }
  101. public bool TryGetValue(TKey key, out TValue value)
  102. {
  103. return this.dictionary.TryGetValue(key, out value);
  104. }
  105. }
  106. }