-
Notifications
You must be signed in to change notification settings - Fork 3
/
Disposer.cs
51 lines (44 loc) · 1.16 KB
/
Disposer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
namespace PyImageSearchSharp
{
public class Disposer : IDisposable
{
private bool _disposed;
private readonly HashSet<IDisposable> _disposableItems = new HashSet<IDisposable>();
public bool Add(IDisposable item)
{
return _disposableItems.Add(item);
}
public void Add(IEnumerable<IDisposable> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
foreach (IDisposable item in items)
{
Add(item);
}
}
private void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
foreach (IDisposable item in _disposableItems)
{
item?.Dispose();
}
_disposableItems.Clear();
}
_disposed = true;
}
~Disposer()
{
Dispose(false);
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}