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 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.ObjectModel; 7 8 namespace System.Net.Mail 9 { 10 /// <summary> 11 /// Summary description for AttachmentCollection. 12 /// </summary> 13 public sealed class AttachmentCollection : Collection<Attachment>, IDisposable 14 { 15 private bool _disposed = false; AttachmentCollection()16 internal AttachmentCollection() { } 17 Dispose()18 public void Dispose() 19 { 20 if (_disposed) 21 { 22 return; 23 } 24 foreach (Attachment attachment in this) 25 { 26 attachment.Dispose(); 27 } 28 Clear(); 29 _disposed = true; 30 } 31 RemoveItem(int index)32 protected override void RemoveItem(int index) 33 { 34 if (_disposed) 35 { 36 throw new ObjectDisposedException(GetType().FullName); 37 } 38 39 base.RemoveItem(index); 40 } 41 ClearItems()42 protected override void ClearItems() 43 { 44 if (_disposed) 45 { 46 throw new ObjectDisposedException(GetType().FullName); 47 } 48 49 base.ClearItems(); 50 } 51 SetItem(int index, Attachment item)52 protected override void SetItem(int index, Attachment item) 53 { 54 if (_disposed) 55 { 56 throw new ObjectDisposedException(GetType().FullName); 57 } 58 59 if (item == null) 60 { 61 throw new ArgumentNullException(nameof(item)); 62 } 63 64 base.SetItem(index, item); 65 } 66 InsertItem(int index, Attachment item)67 protected override void InsertItem(int index, Attachment item) 68 { 69 if (_disposed) 70 { 71 throw new ObjectDisposedException(GetType().FullName); 72 } 73 74 if (item == null) 75 { 76 throw new ArgumentNullException(nameof(item)); 77 } 78 79 base.InsertItem(index, item); 80 } 81 } 82 } 83