识别 IDisposable 对象

发布于 2024-07-15 00:17:09 字数 149 浏览 5 评论 0原文

我必须检查其他人编写的存在内存泄漏的代码。 现在我正在搜索一次性对象以使用 using 语句将它们包围起来,我想知道是否有一种快速方法可以告诉您声明的所有一次性对象。我的意思是像 resharper 或其他 Visual Studio 插件之类的东西。

谢谢。

i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意中人 2024-07-22 00:17:13

另外,根据您是否使用这样的系统,如果您使用 IoC 容器,在通过接口将服务返回给您之前,它可能会经过几层代码,并且在这样的情况下处理 IDisposable 可能并不简单。一个案例。

也许您解析的接口不是继承自IDisposable,但实际使用的服务类继承了? 怎么处理呢? ETC。

Also, depending on whether you use systems like that, if you're using an IoC container, it might go through several layers of code before the service is returned to you through an interface, and it might not be trivial to handle IDisposable in such a case.

Perhaps the interface you resolved doesn't inherit from IDisposable, but the actual service class being used does? How to handle that? etc.

花之痕靓丽 2024-07-22 00:17:13

FxCop 中的使用规则 CA2213 (DisposableFieldsShouldBeDispose) 和 CA2215 (DisposeMethodsShouldCallBaseClassDispose) 将捕获在您自己的类中未正确调用 dispose 的位置,但我不相信有任何东西可以检查 dispose 是否始终被调用,尽管讽刺的是有一条规则( CA2202) 用于 DoNotDisposeObjectsMultipleTimes

Usage Rules CA2213 (DisposableFieldsShouldBeDisposed) and CA2215 (DisposeMethodsShouldCallBaseClassDispose) within FxCop will catch where dispose isn't called correctly in your own classes but i don't believe there is anything out there to check dispose is always called though ironically there is a rule (CA2202) for DoNotDisposeObjectsMultipleTimes

大姐,你呐 2024-07-22 00:17:12

您可以使用 ReSharper 来完成此操作。 借助 ReSharper,您可以使用 Alt-End 轻松导航任何接口的实现,但对于诸如 IDisposable 之类的流行接口来说,这是不切实际的。

您可以执行以下操作:

  1. 转至对象浏览器(Ctrl-Alt-J 或视图 -> 对象浏览器)
  2. 查找 System.IDisposable
  3. 右键单击​​并选择“查找高级用法”(ReSharper 的菜单项)
  4. 用户查找并选中“实现”,在“范围”下选择“解决方案”。
  5. 您将获得实现 IDisposable 的(解决方案的)所有类型的列表。 那些粗体是您想要的 - 它们直接实现IDisposable

希望有帮助。

You could do this with ReSharper. With ReSharper you can navigate implementations of any interface with ease by using Alt-End, but for a popular interface such as IDisposable this is not practical.

Here's what you could do:

  1. Go to Object Browser (Ctrl-Alt-J or View->Object Browser)
  2. Find System.IDisposable
  3. Right click and select "Find Usages Advanced" (ReSharper's menu item)
  4. User Find, check "Implementations", under Scope choose Solution
  5. You will get a list of all types (of your solution) implementing IDisposable. Those in bold are the ones you want - they implement IDisposable directly.

Hope that helps.

绮烟 2024-07-22 00:17:11

我明白你的意思。 我不知道,但看看 FxCop。 它可能在某处有一个规则来检查实现 IDisposable 的对象是否未释放。 只是一种预感,请注意。

更新米奇·小麦写道:

FxCop 包含规则,即从实现 IDisposable 的类型派生的所有类型都应实现 Dispose() 模式

谢谢,Mitch。

I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable are not disposed. Just a hunch, mind.

UPDATE: Mitch Wheat writes:

FxCop includes the rule, thats says all types that derive from types that implement IDisposable should implement the Dispose() pattern

Thanks, Mitch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文