Identify IDisposable objects

戏子无情 提交于 2019-11-30 09:01:19
Neil Barnwell

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.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!