ReadOnlyInteropCollectionHelper.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace WinSCP
  5. {
  6. internal class ReadOnlyInteropCollectionHelper<T> : ICollection<T>
  7. {
  8. public void InternalAdd(T item)
  9. {
  10. _list.Add(item);
  11. }
  12. public void InternalRemoveFirst()
  13. {
  14. _list.RemoveAt(0);
  15. }
  16. public T this[int index]
  17. {
  18. get
  19. {
  20. return _list[index];
  21. }
  22. set
  23. {
  24. throw CreateReadOnlyException();
  25. }
  26. }
  27. #region ICollection<T> Members
  28. public void Add(T item)
  29. {
  30. throw CreateReadOnlyException();
  31. }
  32. public void Clear()
  33. {
  34. throw CreateReadOnlyException();
  35. }
  36. public bool Contains(T item)
  37. {
  38. return _list.Contains(item);
  39. }
  40. public void CopyTo(T[] array, int arrayIndex)
  41. {
  42. _list.CopyTo(array, arrayIndex);
  43. }
  44. public int Count
  45. {
  46. get { return _list.Count; }
  47. }
  48. public bool IsReadOnly
  49. {
  50. get { return true; }
  51. }
  52. public bool Remove(T item)
  53. {
  54. throw CreateReadOnlyException();
  55. }
  56. #endregion
  57. #region IEnumerable<T> Members
  58. public IEnumerator<T> GetEnumerator()
  59. {
  60. return _list.GetEnumerator();
  61. }
  62. #endregion
  63. #region IEnumerable Members
  64. IEnumerator IEnumerable.GetEnumerator()
  65. {
  66. return _list.GetEnumerator();
  67. }
  68. #endregion
  69. private static Exception CreateReadOnlyException()
  70. {
  71. return new InvalidOperationException("Collection is read-only.");
  72. }
  73. private readonly List<T> _list = new List<T>();
  74. }
  75. }