如何实现一个类的处置,该类具有对象作为不实现 IDisposable 的成员?
请参考下面的示例以了解我的意思:
Public Class MyClass
Implements IDisposable
private emp as EmpClass
private dept as DeptClass
' EmpClass and DeptClass doesn't implemented IDisposable interface.
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
emp = Nothing ' --> Can't call Dispose method so I set to null
dept = Nothing ' --> Can't call Dispose method so I set to null
End If
End If
Me.disposedValue = True
End Sub
End Class
从上面看,都是托管对象,我可以做必要的更新。所以请建议该代码是否正常。
已编辑
当我尝试创建 MyClass 的实例时,编译器给了我一个错误,我必须实现 IDisposable。以下是我的调用代码。
Using ctx = MyFactory.CreateMyClass()
'DO Something
End Using