使用.NET对象的IDisposable处理 [英] Dealing with .NET IDisposable objects

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

问题描述

我在C#中工作,我一直很松懈关于使用使用块,宣布实施对象的IDisposable ,其中你总是显然应该做的。不过,我没有看到,当我滑倒了知道一个简单的方法。 Visual Studio中似乎并没有表明这一点以任何方式(我只是失去了一些东西?)。我只是应该检查每一个帮助我宣布任何时间,逐步建立一个百科全书式的记忆哪些对象,哪些不是一次性的?似乎是不必要的,痛苦的,而且容易出错。

I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable, which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to check help every time I declare anything, and gradually build up an encyclopedic memory for which objects are and which are not disposable? Seems unnecessary, painful, and error-prone.

如何的处理呢?

编辑:

综观相关问题侧边栏,我发现的另一个问题其中明确提出,的Dispose()是应该是由对象的终结无论如何调用。所以,即使你从来没有自己称呼它,它应该最终会发生,这意味着你不会有内存泄漏,如果你不使用使用(这是我想我真的很担心一直以来)。唯一需要注意的是,垃圾收集器不知道多少额外的内存正在举行由对象为非托管的东西,所以它不会有一个准确的知道有多少内存将通过收集对象被释放。这将导致被垃圾收集器理想低于平时的表现。

Looking at the related questions sidebar, I found another question which made it clear that Dispose() is supposed to be called by the object's finalizer anyway. So even if you never call it yourself, it should eventually happen, meaning you won't have a memory leak if you don't use using (which is what I suppose I was really worried about all along). The only caveat is that the garbage collector doesn't know how much extra memory is being held by the object as unmanaged stuff, so it won't have an accurate idea how much memory will be freed by collecting the object. This will result in less-ideal-than-usual performance by the garbage collector.

在总之,这是不是世界的末日,如果我错过使用。我只希望事情会产生至少是一个警告

In short, it's not the end of the world if I miss a using. I just wish something would generate at least a warning for it.

(题外话:为什么是有链接到另一个问题,没有什么特别的降价?)

(Off-topic: why is there no special markdown for linking to another question?)

编辑:

好,做工精细,偃旗息鼓。这是超级骗子的所有燃煤戏剧性,花栗鼠 -level 重要调用的Dispose(),否则我们将所有的死亡

Ok, fine, stop clamoring. It's super duper all-fired dramatic-chipmunk-level important to call Dispose() or we'll all die.

现在。鉴于这种情况,为什么会这样容易—地狱,为什么连的允许的—这样做是错误的?你必须去你的出路做是正确的。做它喜欢哈米吉多顿(显然)一切结果。这么多的封装,是吧?

Now. Given that, why is it so easy — hell, why is it even allowed — to do it wrong? You have to go out of your way to do it right. Doing it like everything else results in armageddon (apparently). So much for encapsulation, huh?

[秸秆过,厌恶]

推荐答案

的FxCop的可能的帮助(虽然它没有发现我只是在它发射测试);但肯定的:你是为了检查。 的IDisposable 很简单,你需要进入这个习惯的系统很重要的一部分。使用IntelliSense寻找 .D 是一个良好的开端(虽然不是完美的)。

FxCop might help (although it didn't spot a test I just fired at it); but yes: you are meant to check. IDisposable is simply such an important part of the system that you need to get into this habit. Using intellisense to look for .D is a good start (though not perfect).

然而,这不是' t拍摄长时间来熟悉需要处置的类型;通常任何涉及任何外部(连接,文件,数据库),例如

However, it doesn't take long to familiarize yourself with types that need disposal; generally anything involving anything external (connection, file, database), for example.

ReSharper的做工作也提供了一个投入使用构建选项。它并没有提供它是错的,但...

ReSharper does the job too, offering a "put into using construct" option. It doesn't offer it as an error, though...

当然,如果你不确定 - 的尝试使用它:编译器,如果你正在偏执会在你嘲讽笑:

Of course, if you are unsure - try using it: the compiler will laugh mockingly at you if you are being paranoid:

using (int i = 5) {}

Error   1   'int': type used in a using statement must be implicitly convertible to 'System.IDisposable'    

这篇关于使用.NET对象的IDisposable处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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