简体   繁体   English

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

[英]How to find all Classes implementing IDisposable?

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. 在我的代码中,我注意到几个IDisposable项目没有被处理,并已修复。 However, that leads me to a more basic question, how do I find all classes used in my project that implement IDisposable? 但是,这引出了一个更基本的问题,如何找到我的项目中使用 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). 我已经找到了一个实现IDisposable的不太明显的类(DataTable实现了MarshalByValueComponent,它继承了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? 现在,我通过使用MSDN手动检查任何可疑的类,但是我没有某种方法可以自动执行此过程?

Reflector can show you which classes implement IDisposable : just locate the IDisposable interface in Reflector, and expand the "Derived types" node Reflector可以显示哪些类实现IDisposable :只需在Reflector中找到IDisposable接口,然后展开“派生类型”节点

Another option, from code, is to scan all loaded assemblies for types implementing IDisposable : 代码中的另一个选项是扫描所有已加载的程序集以查找实现IDisposable类型:

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

Test your project with FxCop. 使用FxCop测试您的项目。 It can catch all places where IDisposable objects are not disposed. 它可以捕获未放置IDisposable对象的所有位置。 You may need to do some work to disable all irrelevant FxCop rules, leaving only IDisposable-related rules. 您可能需要做一些工作来禁用所有不相关的FxCop规则,只留下与IDisposable相关的规则。

For example, this is one of FxCop IDisposable rules: http://msdn.microsoft.com/en-us/library/ms182289%28VS.100%29.aspx 例如,这是FxCop IDisposable规则之一: http//msdn.microsoft.com/en-us/library/ms182289%28VS.100%29.aspx

Note: you need to find both your own, .NET and third-party IDisposable objects which are not handled correctly. 注意:您需要找到自己的.NET和第三方IDisposable对象,这些对象未正确处理。

You can use NDepend to analyze your project and find all types that implement IDisposable. 您可以使用NDepend来分析项目并查找实现IDisposable的所有类型。

Here´s the the cql query for the result 这是结果的cql查询

SELECT TYPES WHERE Implement "System.IDisposable"

I think something like the code below might work. 我认为类似下面的代码可能会起作用。 It would have to be adjusted to load the correct assembly if run from an external tool though. 如果从外部工具运行,则必须调整以加载正确的组件。

Assembly asm = Assembly.GetExecutingAssembly();
foreach (Type type in asm.GetTypes())
{
 if(type.GetInterface(typeof(IDisposable).FullName)  != null)
 {
  // Store the list somewhere
 }
}

try this LINQ query: 试试这个LINQ查询:

var allIdisposibles = from Type t in Assembly.GetExecutingAssembly().GetTypes()
                      where t.GetInterface(typeof(IDisposable).FullName) != null && t.IsClass
                      select t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM