PluginCollection.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. namespace log4net.Plugin
  22. {
  23. /// <summary>
  24. /// A strongly-typed collection of <see cref="IPlugin"/> objects.
  25. /// </summary>
  26. /// <author>Nicko Cadell</author>
  27. public class PluginCollection : ICollection, IList, IEnumerable
  28. #if !NETSTANDARD1_3
  29. , ICloneable
  30. #endif
  31. {
  32. #region Interfaces
  33. /// <summary>
  34. /// Supports type-safe iteration over a <see cref="PluginCollection"/>.
  35. /// </summary>
  36. /// <exclude/>
  37. public interface IPluginCollectionEnumerator
  38. {
  39. /// <summary>
  40. /// Gets the current element in the collection.
  41. /// </summary>
  42. IPlugin Current { get; }
  43. /// <summary>
  44. /// Advances the enumerator to the next element in the collection.
  45. /// </summary>
  46. /// <returns>
  47. /// <c>true</c> if the enumerator was successfully advanced to the next element;
  48. /// <c>false</c> if the enumerator has passed the end of the collection.
  49. /// </returns>
  50. /// <exception cref="InvalidOperationException">
  51. /// The collection was modified after the enumerator was created.
  52. /// </exception>
  53. bool MoveNext();
  54. /// <summary>
  55. /// Sets the enumerator to its initial position, before the first element in the collection.
  56. /// </summary>
  57. void Reset();
  58. }
  59. #endregion Interfaces
  60. private const int DEFAULT_CAPACITY = 16;
  61. #region Implementation (data)
  62. private IPlugin[] m_array;
  63. private int m_count = 0;
  64. private int m_version = 0;
  65. #endregion Implementation (data)
  66. #region Static Wrappers
  67. /// <summary>
  68. /// Creates a read-only wrapper for a <c>PluginCollection</c> instance.
  69. /// </summary>
  70. /// <param name="list">list to create a readonly wrapper arround</param>
  71. /// <returns>
  72. /// A <c>PluginCollection</c> wrapper that is read-only.
  73. /// </returns>
  74. public static PluginCollection ReadOnly(PluginCollection list)
  75. {
  76. if(list == null) throw new ArgumentNullException("list");
  77. return new ReadOnlyPluginCollection(list);
  78. }
  79. #endregion
  80. #region Constructors
  81. /// <summary>
  82. /// Initializes a new instance of the <c>PluginCollection</c> class
  83. /// that is empty and has the default initial capacity.
  84. /// </summary>
  85. public PluginCollection()
  86. {
  87. m_array = new IPlugin[DEFAULT_CAPACITY];
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the <c>PluginCollection</c> class
  91. /// that has the specified initial capacity.
  92. /// </summary>
  93. /// <param name="capacity">
  94. /// The number of elements that the new <c>PluginCollection</c> is initially capable of storing.
  95. /// </param>
  96. public PluginCollection(int capacity)
  97. {
  98. m_array = new IPlugin[capacity];
  99. }
  100. /// <summary>
  101. /// Initializes a new instance of the <c>PluginCollection</c> class
  102. /// that contains elements copied from the specified <c>PluginCollection</c>.
  103. /// </summary>
  104. /// <param name="c">The <c>PluginCollection</c> whose elements are copied to the new collection.</param>
  105. public PluginCollection(PluginCollection c)
  106. {
  107. m_array = new IPlugin[c.Count];
  108. AddRange(c);
  109. }
  110. /// <summary>
  111. /// Initializes a new instance of the <c>PluginCollection</c> class
  112. /// that contains elements copied from the specified <see cref="IPlugin"/> array.
  113. /// </summary>
  114. /// <param name="a">The <see cref="IPlugin"/> array whose elements are copied to the new list.</param>
  115. public PluginCollection(IPlugin[] a)
  116. {
  117. m_array = new IPlugin[a.Length];
  118. AddRange(a);
  119. }
  120. /// <summary>
  121. /// Initializes a new instance of the <c>PluginCollection</c> class
  122. /// that contains elements copied from the specified <see cref="IPlugin"/> collection.
  123. /// </summary>
  124. /// <param name="col">The <see cref="IPlugin"/> collection whose elements are copied to the new list.</param>
  125. public PluginCollection(ICollection col)
  126. {
  127. m_array = new IPlugin[col.Count];
  128. AddRange(col);
  129. }
  130. /// <summary>
  131. /// Type visible only to our subclasses
  132. /// Used to access protected constructor
  133. /// </summary>
  134. /// <exclude/>
  135. protected internal enum Tag
  136. {
  137. /// <summary>
  138. /// A value
  139. /// </summary>
  140. Default
  141. }
  142. /// <summary>
  143. /// Allow subclasses to avoid our default constructors
  144. /// </summary>
  145. /// <param name="tag"></param>
  146. /// <exclude/>
  147. protected internal PluginCollection(Tag tag)
  148. {
  149. m_array = null;
  150. }
  151. #endregion
  152. #region Operations (type-safe ICollection)
  153. /// <summary>
  154. /// Gets the number of elements actually contained in the <c>PluginCollection</c>.
  155. /// </summary>
  156. public virtual int Count
  157. {
  158. get { return m_count; }
  159. }
  160. /// <summary>
  161. /// Copies the entire <c>PluginCollection</c> to a one-dimensional
  162. /// <see cref="IPlugin"/> array.
  163. /// </summary>
  164. /// <param name="array">The one-dimensional <see cref="IPlugin"/> array to copy to.</param>
  165. public virtual void CopyTo(IPlugin[] array)
  166. {
  167. this.CopyTo(array, 0);
  168. }
  169. /// <summary>
  170. /// Copies the entire <c>PluginCollection</c> to a one-dimensional
  171. /// <see cref="IPlugin"/> array, starting at the specified index of the target array.
  172. /// </summary>
  173. /// <param name="array">The one-dimensional <see cref="IPlugin"/> array to copy to.</param>
  174. /// <param name="start">The zero-based index in <paramref name="array"/> at which copying begins.</param>
  175. public virtual void CopyTo(IPlugin[] array, int start)
  176. {
  177. if (m_count > array.GetUpperBound(0) + 1 - start)
  178. {
  179. throw new System.ArgumentException("Destination array was not long enough.");
  180. }
  181. Array.Copy(m_array, 0, array, start, m_count);
  182. }
  183. /// <summary>
  184. /// Gets a value indicating whether access to the collection is synchronized (thread-safe).
  185. /// </summary>
  186. /// <returns>false, because the backing type is an array, which is never thread-safe.</returns>
  187. public virtual bool IsSynchronized
  188. {
  189. get { return false; }
  190. }
  191. /// <summary>
  192. /// Gets an object that can be used to synchronize access to the collection.
  193. /// </summary>
  194. /// <value>
  195. /// An object that can be used to synchronize access to the collection.
  196. /// </value>
  197. public virtual object SyncRoot
  198. {
  199. get { return m_array; }
  200. }
  201. #endregion
  202. #region Operations (type-safe IList)
  203. /// <summary>
  204. /// Gets or sets the <see cref="IPlugin"/> at the specified index.
  205. /// </summary>
  206. /// <value>
  207. /// The <see cref="IPlugin"/> at the specified index.
  208. /// </value>
  209. /// <param name="index">The zero-based index of the element to get or set.</param>
  210. /// <exception cref="ArgumentOutOfRangeException">
  211. /// <para><paramref name="index"/> is less than zero.</para>
  212. /// <para>-or-</para>
  213. /// <para><paramref name="index"/> is equal to or greater than <see cref="PluginCollection.Count"/>.</para>
  214. /// </exception>
  215. public virtual IPlugin this[int index]
  216. {
  217. get
  218. {
  219. ValidateIndex(index); // throws
  220. return m_array[index];
  221. }
  222. set
  223. {
  224. ValidateIndex(index); // throws
  225. ++m_version;
  226. m_array[index] = value;
  227. }
  228. }
  229. /// <summary>
  230. /// Adds a <see cref="IPlugin"/> to the end of the <c>PluginCollection</c>.
  231. /// </summary>
  232. /// <param name="item">The <see cref="IPlugin"/> to be added to the end of the <c>PluginCollection</c>.</param>
  233. /// <returns>The index at which the value has been added.</returns>
  234. public virtual int Add(IPlugin item)
  235. {
  236. if (m_count == m_array.Length)
  237. {
  238. EnsureCapacity(m_count + 1);
  239. }
  240. m_array[m_count] = item;
  241. m_version++;
  242. return m_count++;
  243. }
  244. /// <summary>
  245. /// Removes all elements from the <c>PluginCollection</c>.
  246. /// </summary>
  247. public virtual void Clear()
  248. {
  249. ++m_version;
  250. m_array = new IPlugin[DEFAULT_CAPACITY];
  251. m_count = 0;
  252. }
  253. /// <summary>
  254. /// Creates a shallow copy of the <see cref="PluginCollection"/>.
  255. /// </summary>
  256. /// <returns>A new <see cref="PluginCollection"/> with a shallow copy of the collection data.</returns>
  257. public virtual object Clone()
  258. {
  259. PluginCollection newCol = new PluginCollection(m_count);
  260. Array.Copy(m_array, 0, newCol.m_array, 0, m_count);
  261. newCol.m_count = m_count;
  262. newCol.m_version = m_version;
  263. return newCol;
  264. }
  265. /// <summary>
  266. /// Determines whether a given <see cref="IPlugin"/> is in the <c>PluginCollection</c>.
  267. /// </summary>
  268. /// <param name="item">The <see cref="IPlugin"/> to check for.</param>
  269. /// <returns><c>true</c> if <paramref name="item"/> is found in the <c>PluginCollection</c>; otherwise, <c>false</c>.</returns>
  270. public virtual bool Contains(IPlugin item)
  271. {
  272. for (int i=0; i != m_count; ++i)
  273. {
  274. if (m_array[i].Equals(item))
  275. {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. /// <summary>
  282. /// Returns the zero-based index of the first occurrence of a <see cref="IPlugin"/>
  283. /// in the <c>PluginCollection</c>.
  284. /// </summary>
  285. /// <param name="item">The <see cref="IPlugin"/> to locate in the <c>PluginCollection</c>.</param>
  286. /// <returns>
  287. /// The zero-based index of the first occurrence of <paramref name="item"/>
  288. /// in the entire <c>PluginCollection</c>, if found; otherwise, -1.
  289. /// </returns>
  290. public virtual int IndexOf(IPlugin item)
  291. {
  292. for (int i=0; i != m_count; ++i)
  293. {
  294. if (m_array[i].Equals(item))
  295. {
  296. return i;
  297. }
  298. }
  299. return -1;
  300. }
  301. /// <summary>
  302. /// Inserts an element into the <c>PluginCollection</c> at the specified index.
  303. /// </summary>
  304. /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
  305. /// <param name="item">The <see cref="IPlugin"/> to insert.</param>
  306. /// <exception cref="ArgumentOutOfRangeException">
  307. /// <para><paramref name="index"/> is less than zero</para>
  308. /// <para>-or-</para>
  309. /// <para><paramref name="index"/> is equal to or greater than <see cref="PluginCollection.Count"/>.</para>
  310. /// </exception>
  311. public virtual void Insert(int index, IPlugin item)
  312. {
  313. ValidateIndex(index, true); // throws
  314. if (m_count == m_array.Length)
  315. {
  316. EnsureCapacity(m_count + 1);
  317. }
  318. if (index < m_count)
  319. {
  320. Array.Copy(m_array, index, m_array, index + 1, m_count - index);
  321. }
  322. m_array[index] = item;
  323. m_count++;
  324. m_version++;
  325. }
  326. /// <summary>
  327. /// Removes the first occurrence of a specific <see cref="IPlugin"/> from the <c>PluginCollection</c>.
  328. /// </summary>
  329. /// <param name="item">The <see cref="IPlugin"/> to remove from the <c>PluginCollection</c>.</param>
  330. /// <exception cref="ArgumentException">
  331. /// The specified <see cref="IPlugin"/> was not found in the <c>PluginCollection</c>.
  332. /// </exception>
  333. public virtual void Remove(IPlugin item)
  334. {
  335. int i = IndexOf(item);
  336. if (i < 0)
  337. {
  338. throw new System.ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
  339. }
  340. ++m_version;
  341. RemoveAt(i);
  342. }
  343. /// <summary>
  344. /// Removes the element at the specified index of the <c>PluginCollection</c>.
  345. /// </summary>
  346. /// <param name="index">The zero-based index of the element to remove.</param>
  347. /// <exception cref="ArgumentOutOfRangeException">
  348. /// <para><paramref name="index"/> is less than zero.</para>
  349. /// <para>-or-</para>
  350. /// <para><paramref name="index"/> is equal to or greater than <see cref="PluginCollection.Count"/>.</para>
  351. /// </exception>
  352. public virtual void RemoveAt(int index)
  353. {
  354. ValidateIndex(index); // throws
  355. m_count--;
  356. if (index < m_count)
  357. {
  358. Array.Copy(m_array, index + 1, m_array, index, m_count - index);
  359. }
  360. // We can't set the deleted entry equal to null, because it might be a value type.
  361. // Instead, we'll create an empty single-element array of the right type and copy it
  362. // over the entry we want to erase.
  363. IPlugin[] temp = new IPlugin[1];
  364. Array.Copy(temp, 0, m_array, m_count, 1);
  365. m_version++;
  366. }
  367. /// <summary>
  368. /// Gets a value indicating whether the collection has a fixed size.
  369. /// </summary>
  370. /// <value><c>true</c> if the collection has a fixed size; otherwise, <c>false</c>. The default is <c>false</c>.</value>
  371. public virtual bool IsFixedSize
  372. {
  373. get { return false; }
  374. }
  375. /// <summary>
  376. /// Gets a value indicating whether the IList is read-only.
  377. /// </summary>
  378. /// <value><c>true</c> if the collection is read-only; otherwise, <c>false</c>. The default is <c>false</c>.</value>
  379. public virtual bool IsReadOnly
  380. {
  381. get { return false; }
  382. }
  383. #endregion
  384. #region Operations (type-safe IEnumerable)
  385. /// <summary>
  386. /// Returns an enumerator that can iterate through the <c>PluginCollection</c>.
  387. /// </summary>
  388. /// <returns>An <see cref="Enumerator"/> for the entire <c>PluginCollection</c>.</returns>
  389. public virtual IPluginCollectionEnumerator GetEnumerator()
  390. {
  391. return new Enumerator(this);
  392. }
  393. #endregion
  394. #region Public helpers (just to mimic some nice features of ArrayList)
  395. /// <summary>
  396. /// Gets or sets the number of elements the <c>PluginCollection</c> can contain.
  397. /// </summary>
  398. /// <value>
  399. /// The number of elements the <c>PluginCollection</c> can contain.
  400. /// </value>
  401. public virtual int Capacity
  402. {
  403. get
  404. {
  405. return m_array.Length;
  406. }
  407. set
  408. {
  409. if (value < m_count)
  410. {
  411. value = m_count;
  412. }
  413. if (value != m_array.Length)
  414. {
  415. if (value > 0)
  416. {
  417. IPlugin[] temp = new IPlugin[value];
  418. Array.Copy(m_array, 0, temp, 0, m_count);
  419. m_array = temp;
  420. }
  421. else
  422. {
  423. m_array = new IPlugin[DEFAULT_CAPACITY];
  424. }
  425. }
  426. }
  427. }
  428. /// <summary>
  429. /// Adds the elements of another <c>PluginCollection</c> to the current <c>PluginCollection</c>.
  430. /// </summary>
  431. /// <param name="x">The <c>PluginCollection</c> whose elements should be added to the end of the current <c>PluginCollection</c>.</param>
  432. /// <returns>The new <see cref="PluginCollection.Count"/> of the <c>PluginCollection</c>.</returns>
  433. public virtual int AddRange(PluginCollection x)
  434. {
  435. if (m_count + x.Count >= m_array.Length)
  436. {
  437. EnsureCapacity(m_count + x.Count);
  438. }
  439. Array.Copy(x.m_array, 0, m_array, m_count, x.Count);
  440. m_count += x.Count;
  441. m_version++;
  442. return m_count;
  443. }
  444. /// <summary>
  445. /// Adds the elements of a <see cref="IPlugin"/> array to the current <c>PluginCollection</c>.
  446. /// </summary>
  447. /// <param name="x">The <see cref="IPlugin"/> array whose elements should be added to the end of the <c>PluginCollection</c>.</param>
  448. /// <returns>The new <see cref="PluginCollection.Count"/> of the <c>PluginCollection</c>.</returns>
  449. public virtual int AddRange(IPlugin[] x)
  450. {
  451. if (m_count + x.Length >= m_array.Length)
  452. {
  453. EnsureCapacity(m_count + x.Length);
  454. }
  455. Array.Copy(x, 0, m_array, m_count, x.Length);
  456. m_count += x.Length;
  457. m_version++;
  458. return m_count;
  459. }
  460. /// <summary>
  461. /// Adds the elements of a <see cref="IPlugin"/> collection to the current <c>PluginCollection</c>.
  462. /// </summary>
  463. /// <param name="col">The <see cref="IPlugin"/> collection whose elements should be added to the end of the <c>PluginCollection</c>.</param>
  464. /// <returns>The new <see cref="PluginCollection.Count"/> of the <c>PluginCollection</c>.</returns>
  465. public virtual int AddRange(ICollection col)
  466. {
  467. if (m_count + col.Count >= m_array.Length)
  468. {
  469. EnsureCapacity(m_count + col.Count);
  470. }
  471. foreach(object item in col)
  472. {
  473. Add((IPlugin)item);
  474. }
  475. return m_count;
  476. }
  477. /// <summary>
  478. /// Sets the capacity to the actual number of elements.
  479. /// </summary>
  480. public virtual void TrimToSize()
  481. {
  482. this.Capacity = m_count;
  483. }
  484. #endregion
  485. #region Implementation (helpers)
  486. /// <exception cref="ArgumentOutOfRangeException">
  487. /// <para><paramref name="i"/> is less than zero.</para>
  488. /// <para>-or-</para>
  489. /// <para><paramref name="i"/> is equal to or greater than <see cref="PluginCollection.Count"/>.</para>
  490. /// </exception>
  491. private void ValidateIndex(int i)
  492. {
  493. ValidateIndex(i, false);
  494. }
  495. /// <exception cref="ArgumentOutOfRangeException">
  496. /// <para><paramref name="i"/> is less than zero.</para>
  497. /// <para>-or-</para>
  498. /// <para><paramref name="i"/> is equal to or greater than <see cref="PluginCollection.Count"/>.</para>
  499. /// </exception>
  500. private void ValidateIndex(int i, bool allowEqualEnd)
  501. {
  502. int max = (allowEqualEnd) ? (m_count) : (m_count-1);
  503. if (i < 0 || i > max)
  504. {
  505. throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("i", (object)i, "Index was out of range. Must be non-negative and less than the size of the collection. [" + (object)i + "] Specified argument was out of the range of valid values.");
  506. }
  507. }
  508. private void EnsureCapacity(int min)
  509. {
  510. int newCapacity = ((m_array.Length == 0) ? DEFAULT_CAPACITY : m_array.Length * 2);
  511. if (newCapacity < min)
  512. {
  513. newCapacity = min;
  514. }
  515. this.Capacity = newCapacity;
  516. }
  517. #endregion
  518. #region Implementation (ICollection)
  519. void ICollection.CopyTo(Array array, int start)
  520. {
  521. Array.Copy(m_array, 0, array, start, m_count);
  522. }
  523. #endregion
  524. #region Implementation (IList)
  525. object IList.this[int i]
  526. {
  527. get { return (object)this[i]; }
  528. set { this[i] = (IPlugin)value; }
  529. }
  530. int IList.Add(object x)
  531. {
  532. return this.Add((IPlugin)x);
  533. }
  534. bool IList.Contains(object x)
  535. {
  536. return this.Contains((IPlugin)x);
  537. }
  538. int IList.IndexOf(object x)
  539. {
  540. return this.IndexOf((IPlugin)x);
  541. }
  542. void IList.Insert(int pos, object x)
  543. {
  544. this.Insert(pos, (IPlugin)x);
  545. }
  546. void IList.Remove(object x)
  547. {
  548. this.Remove((IPlugin)x);
  549. }
  550. void IList.RemoveAt(int pos)
  551. {
  552. this.RemoveAt(pos);
  553. }
  554. #endregion
  555. #region Implementation (IEnumerable)
  556. IEnumerator IEnumerable.GetEnumerator()
  557. {
  558. return (IEnumerator)(this.GetEnumerator());
  559. }
  560. #endregion Implementation (IEnumerable)
  561. #region Nested enumerator class
  562. /// <summary>
  563. /// Supports simple iteration over a <see cref="PluginCollection"/>.
  564. /// </summary>
  565. /// <exclude/>
  566. private sealed class Enumerator : IEnumerator, IPluginCollectionEnumerator
  567. {
  568. #region Implementation (data)
  569. private readonly PluginCollection m_collection;
  570. private int m_index;
  571. private int m_version;
  572. #endregion Implementation (data)
  573. #region Construction
  574. /// <summary>
  575. /// Initializes a new instance of the <c>Enumerator</c> class.
  576. /// </summary>
  577. /// <param name="tc"></param>
  578. internal Enumerator(PluginCollection tc)
  579. {
  580. m_collection = tc;
  581. m_index = -1;
  582. m_version = tc.m_version;
  583. }
  584. #endregion
  585. #region Operations (type-safe IEnumerator)
  586. /// <summary>
  587. /// Gets the current element in the collection.
  588. /// </summary>
  589. /// <value>
  590. /// The current element in the collection.
  591. /// </value>
  592. public IPlugin Current
  593. {
  594. get { return m_collection[m_index]; }
  595. }
  596. /// <summary>
  597. /// Advances the enumerator to the next element in the collection.
  598. /// </summary>
  599. /// <returns>
  600. /// <c>true</c> if the enumerator was successfully advanced to the next element;
  601. /// <c>false</c> if the enumerator has passed the end of the collection.
  602. /// </returns>
  603. /// <exception cref="InvalidOperationException">
  604. /// The collection was modified after the enumerator was created.
  605. /// </exception>
  606. public bool MoveNext()
  607. {
  608. if (m_version != m_collection.m_version)
  609. {
  610. throw new System.InvalidOperationException("Collection was modified; enumeration operation may not execute.");
  611. }
  612. ++m_index;
  613. return (m_index < m_collection.Count);
  614. }
  615. /// <summary>
  616. /// Sets the enumerator to its initial position, before the first element in the collection.
  617. /// </summary>
  618. public void Reset()
  619. {
  620. m_index = -1;
  621. }
  622. #endregion
  623. #region Implementation (IEnumerator)
  624. object IEnumerator.Current
  625. {
  626. get { return this.Current; }
  627. }
  628. #endregion
  629. }
  630. #endregion
  631. #region Nested Read Only Wrapper class
  632. /// <exclude/>
  633. private sealed class ReadOnlyPluginCollection : PluginCollection
  634. {
  635. #region Implementation (data)
  636. private readonly PluginCollection m_collection;
  637. #endregion
  638. #region Construction
  639. internal ReadOnlyPluginCollection(PluginCollection list) : base(Tag.Default)
  640. {
  641. m_collection = list;
  642. }
  643. #endregion
  644. #region Type-safe ICollection
  645. public override void CopyTo(IPlugin[] array)
  646. {
  647. m_collection.CopyTo(array);
  648. }
  649. public override void CopyTo(IPlugin[] array, int start)
  650. {
  651. m_collection.CopyTo(array,start);
  652. }
  653. public override int Count
  654. {
  655. get { return m_collection.Count; }
  656. }
  657. public override bool IsSynchronized
  658. {
  659. get { return m_collection.IsSynchronized; }
  660. }
  661. public override object SyncRoot
  662. {
  663. get { return this.m_collection.SyncRoot; }
  664. }
  665. #endregion
  666. #region Type-safe IList
  667. public override IPlugin this[int i]
  668. {
  669. get { return m_collection[i]; }
  670. set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
  671. }
  672. public override int Add(IPlugin x)
  673. {
  674. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  675. }
  676. public override void Clear()
  677. {
  678. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  679. }
  680. public override bool Contains(IPlugin x)
  681. {
  682. return m_collection.Contains(x);
  683. }
  684. public override int IndexOf(IPlugin x)
  685. {
  686. return m_collection.IndexOf(x);
  687. }
  688. public override void Insert(int pos, IPlugin x)
  689. {
  690. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  691. }
  692. public override void Remove(IPlugin x)
  693. {
  694. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  695. }
  696. public override void RemoveAt(int pos)
  697. {
  698. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  699. }
  700. public override bool IsFixedSize
  701. {
  702. get { return true; }
  703. }
  704. public override bool IsReadOnly
  705. {
  706. get { return true; }
  707. }
  708. #endregion
  709. #region Type-safe IEnumerable
  710. public override IPluginCollectionEnumerator GetEnumerator()
  711. {
  712. return m_collection.GetEnumerator();
  713. }
  714. #endregion
  715. #region Public Helpers
  716. // (just to mimic some nice features of ArrayList)
  717. public override int Capacity
  718. {
  719. get { return m_collection.Capacity; }
  720. set { throw new NotSupportedException("This is a Read Only Collection and can not be modified"); }
  721. }
  722. public override int AddRange(PluginCollection x)
  723. {
  724. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  725. }
  726. public override int AddRange(IPlugin[] x)
  727. {
  728. throw new NotSupportedException("This is a Read Only Collection and can not be modified");
  729. }
  730. #endregion
  731. }
  732. #endregion
  733. }
  734. }