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

41 lines
935 B
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
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();
}
}