PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Common/Src/Configuration/ConfigurationElementCollection.Silverlight.cs

#
C# | 230 lines | 118 code | 27 blank | 85 comment | 3 complexity | ef649c930e44370ab2c1870471a7d14c MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Core
  4. //===============================================================================
  5. // Copyright Š Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Collections;
  15. using Microsoft.Practices.EnterpriseLibrary.Common.Properties;
  16. namespace Microsoft.Practices.EnterpriseLibrary.Common.Configuration
  17. {
  18. /// <summary>
  19. /// Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
  20. /// </summary>
  21. /// <typeparam name="T">The type of elements in the list.</typeparam>
  22. public class ConfigurationElementCollection<T> : IList<T>, IList
  23. {
  24. private readonly List<T> list = new List<T>();
  25. /// <summary>
  26. /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
  27. /// </summary>
  28. /// <returns>
  29. /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
  30. /// </returns>
  31. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
  32. public int IndexOf(T item)
  33. {
  34. return this.list.IndexOf(item);
  35. }
  36. /// <summary>
  37. /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
  38. /// </summary>
  39. /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param><param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
  40. public void Insert(int index, T item)
  41. {
  42. EnsureCanInsert(item);
  43. this.list.Insert(index, item);
  44. }
  45. /// <summary>
  46. /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
  47. /// </summary>
  48. /// <param name="index">The zero-based index of the item to remove.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
  49. public void RemoveAt(int index)
  50. {
  51. this.list.RemoveAt(index);
  52. }
  53. /// <summary>
  54. /// Gets or sets the element at the specified index.
  55. /// </summary>
  56. /// <returns>
  57. /// The element at the specified index.
  58. /// </returns>
  59. /// <param name="index">The zero-based index of the element to get or set.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
  60. public T this[int index]
  61. {
  62. get { return this.list[index]; }
  63. set { this.list[index] = value; }
  64. }
  65. /// <summary>
  66. /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  67. /// </summary>
  68. /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
  69. public void Add(T item)
  70. {
  71. EnsureCanInsert(item);
  72. this.list.Add(item);
  73. }
  74. /// <summary>
  75. /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  76. /// </summary>
  77. /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
  78. public void Clear()
  79. {
  80. this.list.Clear();
  81. }
  82. /// <summary>
  83. /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
  84. /// </summary>
  85. /// <returns>
  86. /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
  87. /// </returns>
  88. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  89. public bool Contains(T item)
  90. {
  91. return this.list.Contains(item);
  92. }
  93. /// <summary>
  94. /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
  95. /// </summary>
  96. /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type <typeparamref name="T"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
  97. public void CopyTo(T[] array, int arrayIndex)
  98. {
  99. this.list.CopyTo(array, arrayIndex);
  100. }
  101. /// <summary>
  102. /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  103. /// </summary>
  104. /// <returns>
  105. /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  106. /// </returns>
  107. public int Count
  108. {
  109. get { return this.list.Count; }
  110. }
  111. /// <summary>
  112. /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  113. /// </summary>
  114. /// <returns>
  115. /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
  116. /// </returns>
  117. public bool IsReadOnly
  118. {
  119. get { return ((IList<T>)this.list).IsReadOnly; }
  120. }
  121. /// <summary>
  122. /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  123. /// </summary>
  124. /// <returns>
  125. /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
  126. /// </returns>
  127. /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
  128. public bool Remove(T item)
  129. {
  130. return this.list.Remove(item);
  131. }
  132. /// <summary>
  133. /// Returns an enumerator that iterates through the collection.
  134. /// </summary>
  135. /// <returns>
  136. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  137. /// </returns>
  138. public IEnumerator<T> GetEnumerator()
  139. {
  140. return this.list.GetEnumerator();
  141. }
  142. /// <summary>
  143. /// Determines if the item can be inserted into the collection.
  144. /// </summary>
  145. /// <param name="item">The item to check.</param>
  146. /// <returns><see langword="true" /> if the item can be inserted.</returns>
  147. protected virtual bool CanInsert(T item)
  148. {
  149. return true;
  150. }
  151. private void EnsureCanInsert(T item)
  152. {
  153. if (item == null) throw new ArgumentNullException("item");
  154. if (!this.CanInsert(item)) throw new ArgumentException(Resources.ConfigurationElementCollection_CannotInsert, "item");
  155. }
  156. IEnumerator IEnumerable.GetEnumerator()
  157. {
  158. return this.GetEnumerator();
  159. }
  160. int IList.Add(object value)
  161. {
  162. this.Add((T)value);
  163. return this.Count - 1;
  164. }
  165. bool IList.Contains(object value)
  166. {
  167. return this.Contains((T)value);
  168. }
  169. int IList.IndexOf(object value)
  170. {
  171. return this.IndexOf((T)value);
  172. }
  173. void IList.Insert(int index, object value)
  174. {
  175. this.Insert(index, (T)value);
  176. }
  177. bool IList.IsFixedSize
  178. {
  179. get { return ((IList)this.list).IsFixedSize; }
  180. }
  181. void IList.Remove(object value)
  182. {
  183. this.Remove((T)value);
  184. }
  185. object IList.this[int index]
  186. {
  187. get { return this[index]; }
  188. set { this[index] = (T)value; }
  189. }
  190. void ICollection.CopyTo(Array array, int index)
  191. {
  192. ((IList)this.list).CopyTo(array, index);
  193. }
  194. bool ICollection.IsSynchronized
  195. {
  196. get { return ((IList)this.list).IsSynchronized; }
  197. }
  198. object ICollection.SyncRoot
  199. {
  200. get { return ((IList)this.list).SyncRoot; }
  201. }
  202. }
  203. }