ReadOnlyCollection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.Collections.Generic.ReadOnlyCollection
  3. //
  4. // Author:
  5. // Carlos Alberto Cortez ([email protected])
  6. //
  7. // (C) 2004 Carlos Alberto Cortez
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Collections;
  34. using System.Runtime.InteropServices;
  35. namespace System.Collections.Generic
  36. {
  37. [CLSCompliant (false)]
  38. [ComVisible (false)]
  39. public class ReadOnlyCollection <T> : ICollection<T>, IEnumerable<T>, IList<T>,
  40. ICollection, IEnumerable, IList
  41. {
  42. Collection<T> collection;
  43. public ReadOnlyCollection(IList <T> list)
  44. {
  45. collection = new Collection<T> (list, list.Count);
  46. }
  47. public bool Contains (T value)
  48. {
  49. return collection.Contains (value);
  50. }
  51. public void CopyTo (T[] array, int index)
  52. {
  53. collection.CopyTo (array, index);
  54. }
  55. public IEnumerator<T> GetEnumerator()
  56. {
  57. return collection.GetEnumerator ();
  58. }
  59. void ICollection.CopyTo (Array array, int index)
  60. {
  61. ((IList) collection).CopyTo (array, index);
  62. }
  63. void ICollection<T>.Add (T item)
  64. {
  65. throw new NotSupportedException ();
  66. }
  67. void ICollection<T>.Clear ()
  68. {
  69. throw new NotSupportedException ();
  70. }
  71. bool ICollection<T>.Remove (T item)
  72. {
  73. throw new NotSupportedException ();
  74. }
  75. IEnumerator IEnumerable.GetEnumerator ()
  76. {
  77. return ((IEnumerable) collection).GetEnumerator ();
  78. }
  79. int IList.Add (object value)
  80. {
  81. throw new NotSupportedException ();
  82. }
  83. void IList.Clear()
  84. {
  85. throw new NotSupportedException ();
  86. }
  87. bool IList.Contains (object value)
  88. {
  89. return ((IList) collection).Contains (value);
  90. }
  91. int IList.IndexOf (object value)
  92. {
  93. return ((IList) collection).IndexOf (value);
  94. }
  95. void IList.Insert (int index, object value)
  96. {
  97. throw new NotSupportedException ();
  98. }
  99. void IList.Remove (object value)
  100. {
  101. throw new NotSupportedException ();
  102. }
  103. void IList.RemoveAt(int index)
  104. {
  105. throw new NotSupportedException ();
  106. }
  107. void IList<T>.Insert (int index, T item)
  108. {
  109. throw new NotSupportedException ();
  110. }
  111. void IList<T>.RemoveAt (int index)
  112. {
  113. throw new NotSupportedException ();
  114. }
  115. public int IndexOf (T value)
  116. {
  117. return collection.IndexOf (value);
  118. }
  119. public int Count {
  120. get {
  121. return collection.Count;
  122. }
  123. }
  124. bool ICollection.IsSynchronized {
  125. get {
  126. return false;
  127. }
  128. }
  129. object ICollection.SyncRoot {
  130. get {
  131. return ((IList) collection).SyncRoot;
  132. }
  133. }
  134. bool ICollection<T>.IsReadOnly {
  135. get {
  136. return true;
  137. }
  138. }
  139. bool IList.IsReadOnly {
  140. get {
  141. return true;
  142. }
  143. }
  144. bool IList.IsFixedSize {
  145. get {
  146. return true;
  147. }
  148. }
  149. object IList.this [int index] {
  150. get {
  151. return ((IList) collection) [index];
  152. }
  153. set
  154. {
  155. throw new NotSupportedException ();
  156. }
  157. }
  158. public T this [int index] {
  159. get {
  160. return collection [index];
  161. }
  162. set {
  163. throw new NotSupportedException ();
  164. }
  165. }
  166. protected IList<T> Items {
  167. get {
  168. return collection.Items;
  169. }
  170. }
  171. }
  172. }
  173. #endif