VB .Net 2008 - List all objects currently in scope?

感情迁移 提交于 2019-12-24 11:54:08

问题


Please can any one advise me if there is an object in .net that can be used to get a list or references to all objects that are currently in scope for an object. For example if the code is currently executing in a method, what objects declared in this method are currently instanciated and alive and what objects that are declared at class level have been instanciated already and are alive?

Also I know I can test each object declared explicitly for a null reference, but I am looking for something that is more generic (perhaps using reflection?) that can be used from any method to clear up all alive objects.

Thanks in advance,

Best regards,
Duane.


回答1:


I don’t think this information can be retrieved. If it were available, it would probably be available through the System.GC class – but it isn’t.

Reflection can only get you so far – you can inspect objects and with a bit of trickery even local declarations in methods, but you’ve got no easy way of knowing which contexts are active. A stack trace can yield the required information for the (current thread’s) call stack, but this will ignore all static variables in the current application domain (and the System.AppDomain class doesn’t allow access to this information either).



来源:https://stackoverflow.com/questions/3912683/vb-net-2008-list-all-objects-currently-in-scope

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