在 VS 2015 中,当我在实现 IDisposable 的类的类定义上单击“查找所有引用MyBase.Finalize()
”时,它会为每个实现 IDisposable 的类返回。
注意:当我在类定义本身(即Public Class Test123
)上单击“查找所有引用”时会发生这种情况。如果我在实现上单击“查找所有引用”(即New Test123
),那么它只会获取使用该特定New
构造函数的实例。这也令人困惑。在 VS 2010 中,类定义本身和所有实现都组合在一个列表中,但在 2015 年它们不是。
请参阅下面的屏幕截图。即使我将鼠标悬停在类上,Test123
它也会突出MyBase.Finalize()
显示和 Test123
Test456
Visual Studio 2010 不这样做。这是一个错误,还是一个“新功能”?
这造成的困难是,对于当前工作中的项目,我们IDisposable
为每个类手动实现。因此,当我在任何类上单击“查找所有引用”时,需要 10-20 秒才能找到引用,然后它会显示数千个实例MyBase.Finalize()
(每个类一个),它基本上完全没用。
Public Class Form1
Dim a As New Test123
Dim b As New Test456
End Class
Public Class Test123
Implements IDisposable
Dim A As Double = 0
'Dispose Implementation
Dim mbDisposed As Boolean = False
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Private Overloads Sub Dispose(ByVal lbDisposing As Boolean)
If Not mbDisposed Then
If lbDisposing Then
'Dispose of all nullable objects
End If
End If
mbDisposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
End Class
Public Class Test456
Implements IDisposable
Dim B As Double = 1
'Dispose Implementation
Dim mbDisposed As Boolean = False
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Private Overloads Sub Dispose(ByVal lbDisposing As Boolean)
If Not mbDisposed Then
If lbDisposing Then
'Dispose of all nullable objects
End If
End If
mbDisposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
End Class