本文介绍了如何找到所有类implemeting IDisposable的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个大项目,我的任务之一是消除可能的内存泄漏。在我的代码,我注意到没有被设置的几个IDisposable的项目,并有固定的。然而,这使我更基本的问题,我怎么发现的使用的所有类在我的项目,该项目实现IDisposable? (不,已经使用自定义创建的类,但标准库类)。

我已经找到了一个实现IDisposable(数据表工具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?

推荐答案

可以告诉你哪些类实现的IDisposable :刚刚找到在反射器的的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;

这篇关于如何找到所有类implemeting IDisposable的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 13:06