[英]Checking for Objects that implement IDisposable but aren't properly disposed
Is there a way to automatically check existing C# source code for instances of objects that are not properly disposed of ie. 有没有一种方法可以自动检查现有C#源代码中是否存在未正确处置的对象实例。 using try / catch / finally or using statements?
使用try / catch / finally或using语句? Or do I need to just manually look at the code?
还是我只需要手动查看代码?
看看的FxCop为VS2010 -我相信他们恢复了DisposeObjectsBeforeLeavingScope规则-它可以做的正是你想要的。
Use FX Cop for a rule to check if IDisposable are placed in a proper Using block... 使用FX Cop作为规则来检查IDisposable是否放置在适当的Using块中...
You can use reflector to go through the object with IDisposable 您可以使用反射器通过IDisposable浏览对象
Ref.: Is there a list of common object that implement IDisposable for the using statement? 参考: 是否存在用于using语句的实现IDisposable的公共对象的列表? for additional idea
其他想法
HTH 高温超导
CodeRush has some support for spotting obvious variants of this. CodeRush支持发现这种形式的明显变体。 Another possible option is (perhaps via an optional compilation symbol) add a finalizer to your own
IDisposable
objects, and complain loudly if they get finalized without being disposed (set a bool flag in the Dispose
). 另一个可能的选择是(也许通过一个可选的编译符号)向您自己的
IDisposable
对象添加一个终结器,并大声抱怨它们是否被终结而不被处置(在Dispose
设置一个bool标志)。 But note: having a finalizer changes the behaviour of the objects, so don't leave it in production code (or even your regular unit tests). 但是请注意:拥有终结器会更改对象的行为,因此不要将其保留在生产代码(甚至常规的单元测试)中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.