Public static CtoB()
{
Dim Iet1 As IEnumerator
Dim Iet2 As IEnumerator
Dim Idp1 As IDisposable
Try
...
Finally
Idp1 = (Iet1 as IDisposable)
If Not Idp1 Is Nothing Then
Idp1.Dispose()
End If
End Try
{
Dim Iet1 As IEnumerator
Dim Iet2 As IEnumerator
Dim Idp1 As IDisposable
Try
...
Finally
Idp1 = (Iet1 as IDisposable)
If Not Idp1 Is Nothing Then
Idp1.Dispose()
End If
End Try
-----------
Public Shared Sub CtoB() Dim Iet1 As IEnumerator
Dim Iet2 As IEnumerator
Dim Idp1 As IDisposable Try Finally Idp1 = Iet1
If Not Idp1 Is Nothing Then
Idp1.Dispose()
End If End Try
End Sub
http://www.ragingsmurf.com/vbcsharpconverter.aspx
http://www.kamalpatel.net/ConvertCSharp2VB.aspx
http://authors.aspalliance.com/aldotnet/examples/translate.aspx
我尝试Idp1 = ctype(Iet1,IDisposable)也是不行。
用Idp1 = (Iet1 as IDisposable)时,提示“需要)。”从语法上说不过去啊。烦恼中。
请大侠不吝指教。
{
Dim Iet1 As IEnumerator
Dim Iet2 As IEnumerator
Dim Idp1 As IDisposable
Try
...
Finally
Idp1 = (Iet1 as IDisposable)
If Not Idp1 Is Nothing Then
Idp1.Dispose()
End If
End Try
If Not Iet1 Is Nothing Then
Iet1=nothing
End If
IEnumerator接口没有Dispose方法,
如果将Iet1写成继承于IEnumerator和IDisposable的类,Iet1是可以通过Dispose释放它拥有的所有资源的。
下面是.net文档下的例子:
'.NET 框架开发者指南
'实现 Dispose 方法
Public Class BaseResource
Implements IDisposable
' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private Components As Component
' Track whether Dispose has been called.
Private disposed As Boolean = False ' Constructor for the BaseResource Object.
Public Sub New()
' Insert appropriate constructor code here.
End Sub ' Implement Idisposable.
' Do not make this method Overridable.
' A derived class should not be able to override this method.
Overloads Public Sub Dispose()Implements IDisposable.Dispose
Dispose(true)
' Take yourself off of the finalization queue
' to prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub' Dispose(disposing As Boolean) executes in two distinct scenarios.
' If disposing is true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged resources
' can be disposed.
' If disposing equals false, the method has been called by the runtime
' from inside the finalizer and you should not reference other
' objects. Only unmanaged resources can be disposed.
Overloads Protected Overridable Sub Dispose(disposing As Boolean)
' Check to see if Dispose has already been called.
If Not (Me.disposed) Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If (disposing) Then
' Dispose managed resources.
Components.Dispose()
End If
' Release unmanaged resources. If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
' Note that this is not thread safe.
' Another thread could start disposing the object
' after the managed resources are disposed,
' but before the disposed flag is set to true.
End If
Me.disposed = true
End Sub ' This Finalize method will run only if the
' Dispose method does not get called.
' By default, methods are NotOverridable.
' This prevents a derived class from overriding this method.
Overrides Protected Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(false)
End Sub
' Allow your Dispose method to be called multiple times,
' but throw an exception if the object has been disposed.
' Whenever you do something with this class,
' check to see if it has been disposed.
Public Sub DoSomething()
If Me.disposed Then
Throw New ObjectDisposedException()
End if
End Sub
End Class' Design pattern for a derived class.
' Note that this derived class inherently implements the
' IDisposable interface because it is implemented in the base class.
Public Class MyResourceWrapper
Inherits BaseResource
' A managed resource that you add in this derived class.
private addedManaged As ManagedResource
' A native unmanaged resource that you add in this derived class.
private addedNative As NativeResource
' Track whether Dispose has been called.
Private disposed As Boolean = False ' Constructor for the MyResourceWrapper Object.
Public Sub New()
MyBase.New()
' Insert appropriate constructor code here for the
' added resources.
End Sub Protected Overloads Overrides Sub Dispose(disposing As Boolean)
If Not (Me.disposed) Then
Try
If disposing Then
' Release the managed resources you added in
' this derived class here.
addedManaged.Dispose()
End If
' Release the native unmanaged resources you added
' in this derived class here.
CloseHandle(addedNative)
Me.disposed = true
Finally
' Call Dispose on your base class.
MyBase.Dispose(disposing)
End Try
End If
End Sub
End Class
' This derived class does not have a Finalize method
' or a Dispose method without parameters because it
' inherits them from the base class.