-
Notifications
You must be signed in to change notification settings - Fork 3
/
Disposable.cs
66 lines (61 loc) · 2.34 KB
/
Disposable.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
using System;
namespace WinMailParser
{
/// <summary>
/// Utility class that simplifies the usage of <see cref="IDisposable"/>
/// </summary>
public abstract class Disposable : IDisposable
{
/// <summary>
/// Returns <see langword="true"/> if this instance has been disposed of, <see langword="false"/> otherwise
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="Disposable"/> is reclaimed by garbage collection.
/// </summary>
~Disposable()
{
Dispose(false);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
public void Dispose()
{
if ( !IsDisposed )
{
try
{
Dispose(true);
}
finally
{
IsDisposed = true;
GC.SuppressFinalize(this);
}
}
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources. Remember to call this method from your derived classes.
/// </summary>
/// <param name="disposing">
/// Set to <c>true</c> to release both managed and unmanaged resources.
/// Set to <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose( bool disposing ) { }
/// <summary>
/// Used to assert that the object has not been disposed
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown if the object is in a disposed state.</exception>
/// <remarks>The method is to be used by the subclasses in order to provide a simple method for checking the
/// disposal state of the object.</remarks>
protected void AssertDisposed()
{
if ( IsDisposed )
{
string typeName = GetType().FullName;
throw new ObjectDisposedException(typeName, String.Format(System.Globalization.CultureInfo.InvariantCulture, "Cannot access a disposed {0}.", typeName));
}
}
}
}