-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDisposableUtil.cs
More file actions
55 lines (51 loc) · 2.1 KB
/
Copy pathDisposableUtil.cs
File metadata and controls
55 lines (51 loc) · 2.1 KB
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
/////////////////////////////////////////////////////////////////////////////////
// paint.net //
// Copyright (C) dotPDN LLC, Rick Brewster, and contributors. //
// All Rights Reserved. //
/////////////////////////////////////////////////////////////////////////////////
using System;
using System.Runtime.CompilerServices;
namespace PaintDotNet
{
internal static class DisposableUtil
{
/// <summary>
/// Disposes the given object reference, if it is non-null. If the reference is non-null, it will then be set to null.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Free<T>(ref T disposeMe)
where T : class, IDisposable
{
if (disposeMe != null)
{
disposeMe.Dispose();
disposeMe = null;
}
}
/// <summary>
/// Disposes the given object reference, if it is non-null. If the reference is non-null, it will then be set to null.
/// </summary>
/// <param name="callerIsNotFinalizing">
/// Whether or not the caller is in their finalizer. Pass in the value of 'disposing'
/// from the Dispose() method. Otherwise, use the other overload of Free().
/// If this value is false (which will be the case when Dispose(bool) is called from
/// a finalizer), then Dispose() will not be called on disposeMe, but it will still
/// be set to null.
/// </param>
/// <remarks>
/// This overload of Free() should only be used from a Dispose(bool) method.
/// </remarks>
public static void Free<T>(ref T disposeMe, bool callerIsNotFinalizing)
where T : class, IDisposable
{
if (disposeMe != null)
{
if (callerIsNotFinalizing)
{
disposeMe.Dispose();
}
disposeMe = null;
}
}
}
}