ReadOnlyInteropCollectionHelper.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 T this[int index]
  13. {
  14. get
  15. {
  16. return _list[index];
  17. }
  18. set
  19. {
  20. throw CreateReadOnlyException();
  21. }
  22. }
  23. #region ICollection<T> Members
  24. public void Add(T item)
  25. {
  26. throw CreateReadOnlyException();
  27. }
  28. public void Clear()
  29. {
  30. throw CreateReadOnlyException();
  31. }
  32. public bool Contains(T item)
  33. {
  34. return _list.Contains(item);
  35. }
  36. public void CopyTo(T[] array, int arrayIndex)
  37. {
  38. _list.CopyTo(array, arrayIndex);
  39. }
  40. public int Count
  41. {
  42. get { return _list.Count; }
  43. }
  44. public bool IsReadOnly
  45. {
  46. get { return true; }
  47. }
  48. public bool Remove(T item)
  49. {
  50. throw CreateReadOnlyException();
  51. }
  52. #endregion
  53. #region IEnumerable<T> Members
  54. public IEnumerator<T> GetEnumerator()
  55. {
  56. return _list.GetEnumerator();
  57. }
  58. #endregion
  59. #region IEnumerable Members
  60. IEnumerator IEnumerable.GetEnumerator()
  61. {
  62. return _list.GetEnumerator();
  63. }
  64. #endregion
  65. private static Exception CreateReadOnlyException()
  66. {
  67. return new InvalidOperationException("Collection is read-only.");
  68. }
  69. private readonly List<T> _list = new List<T>();
  70. }
  71. }