简体   繁体   中英

Is it possible to check via reflection that type is in using block (Dispose is called)

I need to find automatically all code that not disposed properly. Is it possible to check via reflection that my type N is used inside using statement (Dispose is called)?

No. The closest you could come is to add a finalizer - possibly conditionally so that it's only included for debug builds - which checks whether or not you've been disposed and logs the problem otherwise. (You'd probably want to keep the stack trace on construction in this case, in order to blame the right code.)

Bear in mind that adding finalizers will cause garbage to stick around for longer - although in your Dispose call you could suppress finalization, so correct code wouldn't have a significant penalty, other than generating the stack trace on construction...

Now that's all assuming you want to do things at execution time. There are various static analysis tools (such as the code analysis built into Visual Studio ) which will tell you at build time if it looks like you haven't disposed of everything appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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