计数类的实例 [英] Count Instances of Class
问题描述
我要寻找一种方式来编程得到.NET和Java某一类类型的实例的数量。
I am looking for a way to programatically get the number of instances of a certain class type in .NET and Java.
比方说我有类Foo。我希望能够在同一个进程,让富的所有实例的当前计数。
不过,我不能修改富,所以静态INT具有计数超出。此外,我不能只加我为一些静态列表中的所有实例,并计算。我希望能够,只是说:
Say for example I have class Foo. I want to be able to, in the same process, get a current count of all instance of Foo.
However I cannot modify Foo, so a static int with counting is out. Also I cannot just add all instances I make to some static list and count that. I want to be able to just say:
System.GC.numberOf< Foo >()
什么的。
我一直在寻找通过垃圾收集器,但我找不到任何相关的方法。
I was looking through the garbage collectors but I could not find any relevant methods.
推荐答案
另一种有点奇特的方式做到这一点是使用面向方面的技术,仪器类(ES)有问题的构造函数(S)。看看 AspectJ的,例如。
Another somewhat exotic way to do it would be to use aspect-oriented techniques to instrument the constructor(s) of the class(es) in question. Take a look at AspectJ, for example.
这篇关于计数类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!