forked from tanglujun/redis-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisposeBase.cs
163 lines (146 loc) · 5.78 KB
/
DisposeBase.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Xml.Serialization;
namespace TFP.Redis
{
/// <summary>具有是否已释放和释放后事件的接口</summary>
public interface IDisposable2 : IDisposable
{
/// <summary>是否已经释放</summary>
[XmlIgnore]
Boolean Disposed { get; }
/// <summary>被销毁时触发事件</summary>
event EventHandler OnDisposed;
}
/// <summary>具有销毁资源处理的抽象基类</summary>
/// <example>
/// <code>
/// /// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary>
/// /// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)。
/// /// 因为该方法只会被调用一次,所以该参数的意义不太大。</param>
/// protected override void OnDispose(bool disposing)
/// {
/// base.OnDispose(disposing);
///
/// if (disposing)
/// {
/// // 如果是构造函数进来,不执行这里的代码
/// }
/// }
/// </code>
/// </example>
public abstract class DisposeBase : IDisposable2
{
#region 释放资源
/// <summary>释放资源</summary>
public void Dispose() { Dispose(true); }
[NonSerialized]
private Int32 disposed = 0;
/// <summary>是否已经释放</summary>
[XmlIgnore]
public Boolean Disposed { get { return disposed > 0; } }
/// <summary>被销毁时触发事件</summary>
[field: NonSerialized]
public event EventHandler OnDisposed;
/// <summary>释放资源,参数表示是否由Dispose调用。该方法保证OnDispose只被调用一次!</summary>
/// <param name="disposing"></param>
private void Dispose(Boolean disposing)
{
if (disposed != 0) return;
if (Interlocked.CompareExchange(ref disposed, 1, 0) != 0) return;
//if (XTrace.Debug)
//{
try
{
OnDispose(disposing);
}
catch (Exception ex)
{
//throw;
}
//}
//else
//{
// OnDispose(disposing);
//}
// 只有基类的OnDispose被调用,才有可能是2
if (Interlocked.CompareExchange(ref disposed, 3, 2) != 2) throw new Exception("设计错误,OnDispose应该只被调用一次!代码不应该直接调用OnDispose,而应该调用Dispose。子类重载OnDispose时必须首先调用基类方法!");
}
/// <summary>子类重载实现资源释放逻辑时必须首先调用基类方法</summary>
/// <param name="disposing">从Dispose调用(释放所有资源)还是析构函数调用(释放非托管资源)。
/// 因为该方法只会被调用一次,所以该参数的意义不太大。</param>
protected virtual void OnDispose(Boolean disposing)
{
// 只有从Dispose中调用,才有可能是1
if (Interlocked.CompareExchange(ref disposed, 2, 1) != 1) throw new Exception("设计错误,OnDispose应该只被调用一次!代码不应该直接调用OnDispose,而应该调用Dispose。");
if (disposing)
{
// 释放托管资源
// 告诉GC,不要调用析构函数
GC.SuppressFinalize(this);
}
// 释放非托管资源
OnDisposed?.Invoke(this, EventArgs.Empty);
}
/// <summary>析构函数</summary>
/// <remarks>
/// 如果忘记调用Dispose,这里会释放非托管资源
/// 如果曾经调用过Dispose,因为GC.SuppressFinalize(this),不会再调用该析构函数
/// </remarks>
~DisposeBase() { Dispose(false); }
#endregion
}
/// <summary>销毁助手。扩展方法专用</summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static class DisposeHelper
{
/// <summary>尝试销毁对象,如果有<see cref="IDisposable"/>则调用</summary>
/// <param name="obj"></param>
/// <returns></returns>
public static Object TryDispose(this Object obj)
{
if (obj == null) return obj;
// 列表元素销毁
if (obj is IEnumerable)
{
// 对于枚举成员,先考虑添加到列表,再逐个销毁,避免销毁过程中集合改变
var list = obj as IList;
if (list == null)
{
list = new List<Object>();
foreach (var item in (obj as IEnumerable))
{
if (item is IDisposable) list.Add(item);
}
}
foreach (var item in list)
{
if (item is IDisposable)
{
try
{
//(item as IDisposable).TryDispose();
// 只需要释放一层,不需要递归
// 因为一般每一个对象负责自己内部成员的释放
(item as IDisposable).Dispose();
}
catch { }
}
}
}
// 对象销毁
if (obj is IDisposable)
{
try
{
(obj as IDisposable).Dispose();
}
catch { }
}
return obj;
}
}
}