Files
Common-VS2019/SacsStandardBase/Base/Core/DisposableCollection.cs
T
2021-07-23 14:06:11 +01:00

58 lines
1.4 KiB
C#

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<T> : Collection<T>, IDisposable
where T : IDisposable
{
//-------------------------------------------------------------------
// Construction.
public DisposableCollection() : base() { }
public DisposableCollection(IList<T> 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();
}
}
}
}
//-------------------------------------------------------------------
/// <summary>
/// Add item to the list.
/// <para>Null items are ignored.</para>
/// </summary>
public new void Add (T item)
{
if (item != null)
base.Add(item);
}
//-------------------------------------------------------------------
}
//=======================================================================
}