Innovenergy_trunk/csharp/Lib/Protocols/DBus/Utils/DisposableStack.cs

41 lines
935 B
C#

namespace InnovEnergy.Lib.Protocols.DBus.Utils;
public class DisposableStack : IDisposable
{
private readonly Stack<IDisposable> _Disposables = new Stack<IDisposable>();
private void Dispose(Boolean disposing)
{
if (!disposing)
return;
while (_Disposables.Count > 0)
_Disposables.Pop().Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
internal IDisposable AddDisposable(IDisposable disposable)
{
_Disposables.Push(disposable);
return disposable;
}
internal IDisposable AddDisposeAction(Action dispose)
{
return AddDisposable(new Disposable(dispose));
}
private class Disposable : IDisposable
{
private readonly Action _Dispose;
public Disposable(Action dispose) => _Dispose = dispose;
public void Dispose() => _Dispose();
}
}