FreezableCollection.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //----------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------
  4. namespace System.Collections.ObjectModel
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel;
  9. class FreezableCollection<T> : Collection<T>, ICollection<T>
  10. {
  11. bool frozen;
  12. public FreezableCollection()
  13. : base()
  14. {
  15. }
  16. public FreezableCollection(IList<T> list)
  17. : base(list)
  18. {
  19. }
  20. public bool IsFrozen
  21. {
  22. get
  23. {
  24. return this.frozen;
  25. }
  26. }
  27. bool ICollection<T>.IsReadOnly
  28. {
  29. get
  30. {
  31. return this.frozen;
  32. }
  33. }
  34. public void Freeze()
  35. {
  36. this.frozen = true;
  37. }
  38. protected override void ClearItems()
  39. {
  40. ThrowIfFrozen();
  41. base.ClearItems();
  42. }
  43. protected override void InsertItem(int index, T item)
  44. {
  45. ThrowIfFrozen();
  46. base.InsertItem(index, item);
  47. }
  48. protected override void RemoveItem(int index)
  49. {
  50. ThrowIfFrozen();
  51. base.RemoveItem(index);
  52. }
  53. protected override void SetItem(int index, T item)
  54. {
  55. ThrowIfFrozen();
  56. base.SetItem(index, item);
  57. }
  58. void ThrowIfFrozen()
  59. {
  60. if (this.frozen)
  61. {
  62. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadOnly));
  63. }
  64. }
  65. }
  66. }