ReadOnlyDictionary.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. namespace Google.ProtocolBuffers.Collections {
  20. /// <summary>
  21. /// Read-only wrapper around another dictionary.
  22. /// </summary>
  23. public sealed class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
  24. readonly IDictionary<TKey, TValue> wrapped;
  25. public ReadOnlyDictionary(IDictionary<TKey, TValue> wrapped) {
  26. this.wrapped = wrapped;
  27. }
  28. public void Add(TKey key, TValue value) {
  29. throw new InvalidOperationException();
  30. }
  31. public bool ContainsKey(TKey key) {
  32. return wrapped.ContainsKey(key);
  33. }
  34. public ICollection<TKey> Keys {
  35. get { return wrapped.Keys; }
  36. }
  37. public bool Remove(TKey key) {
  38. throw new InvalidOperationException();
  39. }
  40. public bool TryGetValue(TKey key, out TValue value) {
  41. return wrapped.TryGetValue(key, out value);
  42. }
  43. public ICollection<TValue> Values {
  44. get { return wrapped.Values; }
  45. }
  46. public TValue this[TKey key] {
  47. get {
  48. return wrapped[key];
  49. }
  50. set {
  51. throw new InvalidOperationException();
  52. }
  53. }
  54. public void Add(KeyValuePair<TKey, TValue> item) {
  55. throw new InvalidOperationException();
  56. }
  57. public void Clear() {
  58. throw new InvalidOperationException();
  59. }
  60. public bool Contains(KeyValuePair<TKey, TValue> item) {
  61. return wrapped.Contains(item);
  62. }
  63. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
  64. wrapped.CopyTo(array, arrayIndex);
  65. }
  66. public int Count {
  67. get { return wrapped.Count; }
  68. }
  69. public bool IsReadOnly {
  70. get { return true; }
  71. }
  72. public bool Remove(KeyValuePair<TKey, TValue> item) {
  73. throw new InvalidOperationException();
  74. }
  75. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
  76. return wrapped.GetEnumerator();
  77. }
  78. IEnumerator IEnumerable.GetEnumerator() {
  79. return ((IEnumerable) wrapped).GetEnumerator();
  80. }
  81. public override bool Equals(object obj) {
  82. return wrapped.Equals(obj);
  83. }
  84. public override int GetHashCode() {
  85. return wrapped.GetHashCode();
  86. }
  87. public override string ToString() {
  88. return wrapped.ToString();
  89. }
  90. }
  91. }