问题描述
...如果我在一个局部变量使用IDisposable接口,但不调用Dispose()或使用using()模式。
... if I use an IDisposable in a local variable, but do not call Dispose() or use the using() pattern.
public void BadMethod()
{
var fs = new FileStream("file.txt", FileMode.Create);
fs.WriteByte(0x55);
// no dispose, no using()
}
Just like the "Types that own disposable fields should be disposable" rule for fields.
编辑:替换通过的FileStream的MemoryStream,因为MemoryStream的只是分配内存,并且不使用(非托管)的资源,所以有人的可以的讨论有关强制性的Dispose()调用
Replaced MemoryStream by FileStream, because MemoryStream just allocates memory and doesn't use (unmanaged) resources, so someone could discuss about a mandatory Dispose() call.
推荐答案
是否有一个的FxCop规则呢?是的,没有。
Is there an FxCop rule for this? Yes and no.
在FxCop的1.35,这是Visual Studio 2005的code分析是基于,有一个规则,DisposeObjectsBeforeLosingScope这正是这一点做到了。
In FxCop 1.35, which is what Visual Studio 2005 Code Analysis is based on, there was a rule DisposeObjectsBeforeLosingScope which did exactly this.
在FxCop的1.36(Visual Studio 2008的code分析),他们去除了他们的数据流分析引擎,这意味着这条规则也不得不被删除。
In FxCop 1.36 (Visual Studio 2008 Code Analysis), they removed their data flow analysis engine, which meant that this rule also had to be removed.
然而,在接下来的FxCop(的Visual Studio 2010 code分析),似乎DisposeObjectsBeforeLosingScope已经回来了!
However, in the next FxCop (Visual Studio 2010 Code Analysis), it seems that DisposeObjectsBeforeLosingScope has returned!
这篇关于是否有一个的FxCop规则本地使用的IDisposable的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!