JD2022-TU1/main/tools/framework/JD.Collections/DisposableList.cs

105 lines
No EOL
2.2 KiB
C#

using System;
using System.Collections.Generic;
using JD.Kernel.Classes;
namespace JD.Collections
{
/// <summary>
/// A list that disposes all of its items in its disposal.
/// </summary>
public class DisposableList : DisposableList<IDisposable>
{
#region Constructor
public DisposableList()
{
}
public DisposableList(IDisposable first, params IDisposable[] elements)
: base(first, elements)
{ }
#endregion
#region Static Functions
public static DisposableList<T> FromEnumerable<T>(IEnumerable<T> disposableList)
where T : IDisposable
{
var result = new DisposableList<T>();
result.AddRange(disposableList);
return result;
}
#endregion
}
/// <summary>
/// A list that disposes all of its items in its disposal.
/// </summary>
public class DisposableList<T> : List<T>, IDisposable
where T : IDisposable
{
#region Constructor
public DisposableList()
{
}
public DisposableList(T first, params T[] elements)
{
Add(first);
AddRange(elements);
}
/// <summary>
/// Ctor.
/// </summary>
/// <param name="elements"></param>
public DisposableList(params T[] elements)
: base(elements)
{ }
/// <summary>
/// Ctor.
/// </summary>
/// <param name="elements"></param>
public DisposableList(IEnumerable<T> elements)
: base(elements)
{ }
#endregion
#region Properties
/// <summary>
/// Gets the last inserted element.
/// </summary>
public T Last
{
get
{
if (Count > 0)
{
return this[Count - 1];
}
return default(T);
}
}
#endregion
#region Methods
/// <summary>
/// Dispose.
/// </summary>
public void Dispose()
{
DisposableHelper.SafeDisposeList(this);
Clear();
}
#endregion
}
}