如何找到所有实现IDisposable的类?

编程入门 行业动态 更新时间:2024-10-09 03:21:11
本文介绍了如何找到所有实现IDisposable的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在处理一个大型项目,我的任务之一是消除可能的内存泄漏.在我的代码中,我注意到有几个IDisposable项没有被丢弃,并已将其修复.但是,这引出了一个更基本的问题,即如何在我的项目中找到实现IDisposable的所有已使用类? (不是自定义创建的类,而是已使用的标准库类). 我已经发现了一个不那么明显的类,它实现了IDisposable(DataTable实现了MarshalByValueComponent,它继承了IDisposable).现在,我正在使用MSDN手动检查任何可疑的类,但是没有某种方法可以使该过程自动化?

I am working on a large project, and one of my tasks is to remove possible memory leaks. In my code, I have noticed several IDisposable items not being disposed of, and have fixed that. However, that leads me to a more basic question, how do I find all classes used in my project that implement IDisposable? (Not custom-created classes but standard Library classes that have been used). I have already found one less-than-obvious class that implements IDisposable ( DataTable implements MarshalByValueComponent, which inherits IDisposable). Right now, I am manually checking any suspected classes by using MSDN, but isn't there some way through which I can automate this process?

推荐答案

Reflector 可以向您显示哪些类实现了IDisposable:只需在Reflector中找到IDisposable接口,然后展开派生类型"节点

Reflector can show you which classes implement IDisposable : just locate the IDisposable interface in Reflector, and expand the "Derived types" node

代码中的另一个选项是扫描所有已加载的程序集以查找实现IDisposable的类型:

Another option, from code, is to scan all loaded assemblies for types implementing IDisposable :

var disposableTypes = from a in AppDomain.CurrentDomain.GetAssemblies() from t in a.GetTypes() where typeof(IDisposable).IsAssignableFrom(t) select t;

更多推荐

如何找到所有实现IDisposable的类?

本文发布于:2023-11-07 15:40:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IDisposable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!