-
Notifications
You must be signed in to change notification settings - Fork 4
/
Disposer.cs
77 lines (63 loc) · 1.54 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
#endregion
/// <summary>
/// Provides a way to easily invoke a clean-up method from a using statement.
/// </summary>
/// <remarks>
/// This was inspired by a similar class in Jeffrey Richter's PowerThreading library.
/// </remarks>
public sealed class Disposer : IDisposable
{
#region Private Data Members
private Action? disposeMethod;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance.
/// </summary>
/// <remarks>
/// See the <see cref="Disposer"/> class comments for an example of using an anonymous
/// dispose method.
/// </remarks>
/// <param name="disposeMethod">The method to invoke during disposal.</param>
public Disposer(Action disposeMethod)
{
Conditions.RequireReference(disposeMethod, nameof(disposeMethod));
this.disposeMethod = disposeMethod;
}
#endregion
#region Public Properties
/// <summary>
/// Gets whether <see cref="Dispose"/> has already been called on this instance.
/// </summary>
public bool IsDisposed
{
get
{
bool result = this.disposeMethod == null;
return result;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Calls the dispose method that was passed to the constructor.
/// </summary>
public void Dispose()
{
if (this.disposeMethod != null)
{
this.disposeMethod();
this.disposeMethod = null;
}
}
#endregion
}
}