vb.net 在一个简单的类上实现 IDisposable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32548395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:27:15  来源:igfitidea点击:

Implementing IDisposable on a simple class

vb.net

提问by user81993

I wanted to make a helper class for my MySql wrapper. The idea is that the methods that encapsulate mysql command construction and execution have an optional MySqlConnection as an argument. If a specific connection is passed, it uses that, if not, it creates one and disposes of it once done. To save 4 lines off every method, I could use this class in the using block and pass the optional argument as a construction parameter. Anyway, heres the class:

我想为我的 MySql 包装器制作一个辅助类。这个想法是封装 mysql 命令构造和执行的方法有一个可选的 MySqlConnection 作为参数。如果传递了一个特定的连接,它会使用它,如果没有,它会创建一个并在完成后处理它。为了在每个方法中节省 4 行,我可以在 using 块中使用这个类并将可选参数作为构造参数传递。无论如何,这是课程:

Public Class DynaConnection
    Implements IDisposable

    Public Dynamic As Boolean
    Public Connection As MySqlConnection
    Public Sub New(Connection As MySqlConnection)
        If Connection Is Nothing Then
            Dynamic = True
            Me.Connection = Connect()
        Else
            Dynamic = False
        End If
    End Sub
    Public Shared Widening Operator CType(ByVal Connection As DynaConnection) As MySqlConnection
        Return Connection.Connection
    End Operator

    Public Sub Dispose() Implements IDisposable.Dispose
        If Dynamic Then
            Connection.Close()
            Connection.Dispose()
        End If
        GC.SuppressFinalize(Me)
    End Sub
End Class

When I first wrote the letters "Implements IDisposable" though, a whole wall of code jumped into the class. I looked at msdn to see whats what but over there was an even longer bunch of code on how to "properly" implement IDisposable.

然而,当我第一次写“Implements IDisposable”这几个字母时,一整面代码都跳进了课堂。我查看了 msdn 想知道是什么,但那边有更多关于如何“正确”实现 IDisposable 的代码。

From what I remember from writing simple IDisposable classes before, what I've done in the class above should suffice. Has something changed?

根据我之前编写简单的 IDisposable 类的记忆,我在上面的类中所做的应该足够了。有什么改变了吗?

回答by dbasnett

This is that "wall of code" with some additional comments.

这是带有一些附加注释的“代码墙”。

' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).

            'If your class holds references to other .NET objects 
            'that themselves implement IDisposable then you should implement IDisposable 
            'and call their Dispose method in yours

        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.

        'If you're holding any OS resources, e.g. file or image handles, 
        'then you should release them. That will be a rare thing for most people and can be pretty much ignored

        ' TODO: set large fields to null.
        'If any of your fields may refer to objects that occupy 
        'a large amount of memory then those fields should be set to Nothing

    End If
    Me.disposedValue = True
End Sub

' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
'    Dispose(False)
'    MyBase.Finalize()
'End Sub

' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub

I agree with GSeng, this is the correct way to implement IDisposable.

我同意 GSeng,这是实施 IDisposable 的正确方法。

回答by Joe Who

I don't know if it is important to the poster but when I added "Implements IDisposable" to my class today, the created code had the line GC.SuppressFinalize(Me) commented out.

我不知道这对海报是否重要,但是当我今天在班级中添加“Implements IDisposable”时,创建的代码将 GC.SuppressFinalize(Me) 行注释掉了。

It was not commented out in the answer. If it should be active delete this post too. If it should be comment out you might want to fix the answer.

答案中没有注释掉。如果它应该是活跃的,也删除这个帖子。如果它应该被注释掉,你可能想要修复答案。