我们都知道 System.IDisposable 模式。它被描述了无数次,也在 StackOverflow 上:
Disposable 模式建议我应该只在我的对象被处置时才处置托管资源,而不是在 finalize 期间
您可以看到发生这种情况是因为建议使用以下代码:
protected void Dispose(bool disposing)
{
if (disposing)
{
// Code to dispose the managed resources of the class
}
// Code to dispose the un-managed resources of the class
}
我知道只要我的类有一个实现 System.IDisposable 的(私有)成员,我的类就应该实现 System.IDisposable。如果布尔处理为真,则 Dispose(bool) 应调用私有成员的 Dispose()。
如果在 finalize 期间调用 Dispose 为什么会出现问题?那么,如果在 finalize 期间调用了以下 Dispose,为什么会出现问题呢?
protected void Dispose(bool disposing)
{
if (myDisposableObject != null)
{
myDisposableObject.Dispose();
myDisposableObject = null;
}
}