RaspberryIO_26/Swan.Lite/Collections/DisposableComponentCollection`1.cs
2019-12-08 19:54:52 +01:00

46 lines
1.5 KiB
C#

using System;
namespace Swan.Collections {
/// <summary>
/// <para>Implements a collection of components that automatically disposes each component
/// implementing <see cref="IDisposable"/>.</para>
/// <para>Each component in the collection may be given a unique name for later retrieval.</para>
/// </summary>
/// <typeparam name="T">The type of components in the collection.</typeparam>
/// <seealso cref="ComponentCollection{T}" />
/// <seealso cref="IComponentCollection{T}" />
public class DisposableComponentCollection<T> : ComponentCollection<T>, IDisposable {
/// <summary>
/// Finalizes an instance of the <see cref="DisposableComponentCollection{T}"/> class.
/// </summary>
~DisposableComponentCollection() {
this.Dispose(false);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> to release both managed and unmanaged resources; <see langword="true"/> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(Boolean disposing) {
if(!disposing) {
return;
}
foreach(T component in this) {
if(component is IDisposable disposable) {
disposable.Dispose();
}
}
}
}
}