| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF |
| | | 9 | | { |
| | | 10 | | internal class FreezableCollection<T> : Collection<T>, ICollection<T> |
| | | 11 | | { |
| | | 12 | | private bool _frozen; |
| | | 13 | | |
| | | 14 | | public FreezableCollection() |
| | 277 | 15 | | : base() |
| | | 16 | | { |
| | 277 | 17 | | } |
| | | 18 | | |
| | | 19 | | public FreezableCollection(IList<T> list) |
| | 0 | 20 | | : base(list) |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public bool IsFrozen |
| | | 25 | | { |
| | | 26 | | get |
| | | 27 | | { |
| | 83 | 28 | | return _frozen; |
| | | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | bool ICollection<T>.IsReadOnly |
| | | 33 | | { |
| | | 34 | | get |
| | | 35 | | { |
| | 0 | 36 | | return _frozen; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public void Freeze() |
| | | 41 | | { |
| | 277 | 42 | | _frozen = true; |
| | 277 | 43 | | } |
| | | 44 | | |
| | | 45 | | protected override void ClearItems() |
| | | 46 | | { |
| | 0 | 47 | | ThrowIfFrozen(); |
| | 0 | 48 | | base.ClearItems(); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | protected override void InsertItem(int index, T item) |
| | | 52 | | { |
| | 397 | 53 | | ThrowIfFrozen(); |
| | 397 | 54 | | base.InsertItem(index, item); |
| | 397 | 55 | | } |
| | | 56 | | |
| | | 57 | | protected override void RemoveItem(int index) |
| | | 58 | | { |
| | 0 | 59 | | ThrowIfFrozen(); |
| | 0 | 60 | | base.RemoveItem(index); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | protected override void SetItem(int index, T item) |
| | | 64 | | { |
| | 0 | 65 | | ThrowIfFrozen(); |
| | 0 | 66 | | base.SetItem(index, item); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private void ThrowIfFrozen() |
| | | 70 | | { |
| | 397 | 71 | | if (_frozen) |
| | | 72 | | { |
| | 0 | 73 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 74 | | } |
| | 397 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |