如何找到所有实现IDisposable的类? [英] How to find all Classes implementing IDisposable?

查看:89
本文介绍了如何找到所有实现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的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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