.Net类适用于有目的性 [英] .Net Class to apply idisposeable

查看:83
本文介绍了.Net类适用于有目的性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我怀疑在类中使用IDisposeable.我尝试将以下代码用于dispose方法.

Hello all pro,

i have a doubt to use the IDisposeable in the class. i try to use the following code for the dispose method.

Public Overloads Sub Dispose() Implements IDisposable.Dispose
    Me.Dispose(True)
    GC.SuppressFinalize(Me) 
End Sub

Protected Overridable Overloads Sub Dispose(ByVal Disposing As Boolean)
    If Not _bDisposed Then
        If Disposing Then
        'manage unmanage resource
        End If

        If _sqlCon IsNot Nothing Then _bDisposed = True
    End If
End Sub

Protected Overrides Sub Finalize()
    Dispose(False)
    MyBase.Finalize()
End Sub


我将在Dispose(Boolean)中清理相关资源.

I)当我们实现IDisposable.Dispose时,这意味着如果已调用dispose方法,则将处理当前实例?

II)如果我确实抑制了finalize,是否会影响到该类本身的处置?

III)即使我在Dispose方法中限制了finalise,但是如果有任何其他指向该类的类,它仍将调用Finalize方法.为什么?

IV)我们知道可以完成finalize的工作以释放资源并执行其他清理操作,但是为什么我们仍然需要进行preventfinalize?


i will clean up the related resource in Dispose(Boolean).

I) when we implements IDisposable.Dispose it is means current instance will disposed if the dispose method has been invoked?

II) if i do suppressfinalize is it this will affect dispose the class itself?

III) even if i supressfinalise in the Dispose Method but if have any others class that are point to this class then it still will invoke Finalize method. why?

IV) we know finalize do free resources and perform other cleanup operations, but why we still need to do the suppressfinalize?

推荐答案

AI)如果您只是实现IDisposable , 什么都没发生.需要使用此方法来保证基于公共接口的某些操作.它的主要用途是使用using语句:
http://msdn.microsoft.com/en-us/library/yh598w02%28v = vs.110%29.aspx [ ^ ].

AII)没有布置课的事情.那应该是什么意思?您需要更好地了解什么是类型,什么是对象.此外,如您从上一项应该理解的那样,处置与定稿无关.

AIII)可能永远不会调用Dispose,因此也永远不会调用GC.SuppressFinalize(Me).在您的代码示例中,没有任何迹象表明您曾经调用过它.在这条线上放置一个断点,看看发生了什么.

AIV)这只是在这里解释的机制: http://msdn.microsoft.com/zh-cn/library/system.gc.suppressfinalize.aspx [
AI) If you just implement IDisposable, nothing happens. This method is needed to guarantee some action based on common interface. The main use of it is using the using statement:
http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.110%29.aspx[^].

AII) There is no such thing as disposing a class. What would it supposed to mean? You need to get better understanding on what a type is, and what is an object. Besides, as you should understand from the previous item, disposal has nothing to do with finalization.

AIII) Probably Dispose is never called, so GC.SuppressFinalize(Me) is also never called. In your code samples, there is no indication you ever call it. Put a break point on this line to see what''s going on.

AIV) This is just the mechanism explained here: http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx[^]. I don''t think using it can be a good programming practice at all. I would speculate this mechanism is included just for completeness of the managed garbage collection and finalization system. Even the destructors are rarely required.

—SA


这篇关于.Net类适用于有目的性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆