ReadOnlyCollection.cs 4.2 KB

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