using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Text; namespace Sarand.Net.Standard.Base.Core { //======================================================================= public sealed class DisposableCollection : Collection, IDisposable where T : class, IDisposable { //------------------------------------------------------------------- // Construction. public DisposableCollection() : base() { } public DisposableCollection(IList items) : base(items) { } public void Dispose() { while (this.Count > 0) { // Extract the first item from the list. T item = this[0]; RemoveAt(0); try { if (item != null) item.Dispose(); } catch (Exception exc) { // Ignore exception. if (SacsSystem.DebugMode) { SaLogger.Debug(GetType().Name + ".Dispose " + ExceptionUtil.GetErrorStateMessage(exc)); Debugger.Break(); } } } } //------------------------------------------------------------------- /// /// Add item to the list. /// Null items are ignored. /// public new void Add(T item) { if (item != null && !Contains(item)) base.Add(item); } /// /// Add list of items to the collection. /// Null items are ignored. /// Items that can't be cast to the required object type cause a debug break. /// public void AddRange(List items) where O : class, IDisposable { if (items != null && !items.IsEmpty()) { foreach (O item in items) { if (item == null) continue; T obj = item as T; if (obj != null) Add(obj); else if (SacsSystem.DebugMode) Debugger.Break(); } } } //------------------------------------------------------------------- } //======================================================================= }