0
如何實現處理一個類的對象作爲不實現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
問題是:爲什麼你實現'IDisposable'呢? – sloth
好問題,我附上我的上面的實際問題,回答你的問題。 –
後續問題:爲什麼你想用'Using'語句來使用你的類? – sloth