VB的.Net 2008 - 当前列表中的作用域的所有对象?对象、作用、列表中、VB

2023-09-07 09:35:38 作者:攒袋星星

请任何一个可以告诉我,如果在.NET中的对象,可以用来获取列表或引用当前在范围对象的所有对象。例如,如果code当前正在执行的方法,什么对象在这个方法中声明目前实例化,充满活力,哪些对象是在类级别上声明已经被实例化已经和还活着?

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.

在此先感谢,

最好的问候, 杜安。

Best regards, Duane.

推荐答案

我不认为这些信息可以检索。如果是可用的,它很可能是可以通过 System.GC的 类 - 但它不是

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.

反射只能让你至今 - 你可以检查对象,并在方法有点取巧,甚至局部声明,但你知道的没有简单的方法哪个上下文是活跃的。堆栈跟踪可以产生的(当前线程的)调用堆栈所需的信息,但这样会忽略在当前应用程序域中的所有静态变量(和的 System.AppDomain 类不允许访问这些信息要么)。

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).