在类方法内计算类类型的对象数 [英] Count number of objects of class type within class method

查看:114
本文介绍了在类方法内计算类类型的对象数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算该类的方法中类类型的对象数量?对于这个问题,如何在一个类外添加对象而不添加对象到列表中?

How can I count the number of objects of a class type within a method of that class? For that matter, how to do it outside of a class without adding the objects to a list?

我应该想到的!谢谢!我要离开它一会儿,看看有没有更好的方法,因为我同意。我只是sortv把我的头围绕OO。如果你不介意让我多说一点,也许一般有更好的方法?

我有一个对象类我想添加3件信息,但首先我想循环通过,并确保没有任何其他对象与任何三个相同,如果有,做一些不同的每种情况。

推荐答案

实现你所需要的唯一方法是在类本身中保留这些对象的静态列表。如果你只是想看看是否有一个实例没有被垃圾收集,那么你将需要使用 WeakReference 类。例如...

The only way to accomplish what you're looking for is to keep a static list of these objects in the class itself. If you just want to see if there is an instance somewhere that hasn't been garbage collected, then you'll want to use the WeakReference class. For example...

public class MyClass
{
    private static List<WeakReference> instances = new List<WeakReference>();

    public MyClass()
    {
         instances.Add(new WeakReference(this));
    }

    public static IList<MyClass> GetInstances()
    {
        List<MyClass> realInstances = new List<MyClass>();
        List<WeakReference> toDelete = new List<WeakReference>();

        foreach(WeakReference reference in instances)
        {
            if(reference.IsAlive)
            {
                realInstances.Add((MyClass)reference.Target);
            }
            else
            {
                toDelete.Add(reference);
            }
        }

        foreach(WeakReference reference in toDelete) instances.Remove(reference);

        return realInstances;
    }
}

由于你刚接触OO /不要让 WeakReference 使用吓唬你。垃圾收集工作的方式是通过引用计数。只要某些代码或对象可以访问特定的实例(意味着它在作为一个或作为局部,实例或静态变量的一部分的范围内),那么该对象被认为是活的。一旦该变量超出范围,在某些时间点,垃圾收集器可以/将收集它。但是,如果您要维护所有引用的列表,它们将永远不会超出范围,因为它们将作为该列表中的引用存在。 WeakReference 是一个特殊的类,允许您维护对垃圾回收器将忽略的对象的引用。 IsAlive 属性指示 WeakReference 是否指向仍然存在的有效对象。

Since you're new to OO/.NET, don't let the WeakReference use scare you. The way garbage collection works is by reference counting. As long as some piece of code or an object has access to a particular instance (meaning it's within scope as a or as part of a local, instance, or static variable) then that object is considered alive. Once that variable falls OUT of scope, at some point after that the garbage collector can/will collect it. However, if you were to maintain a list of all references, they would never fall out of scope since they would exist as references in that list. The WeakReference is a special class allows you to maintain a reference to an object that the garbage collector will ignore. The IsAlive property indicates whether or not the WeakReference is pointing to a valid object that still exists.

所以我们这里做的是保存 WeakReference 的列表,指向 MyClass 已创建。当你想要获得它们的列表时,我们遍历我们的 WeakReference s并抓取所有活着的。任何我们发现不再活的,放入另一个临时列表,以便我们可以从外部列表中删除它们(这样可以收集 WeakReference 类本身,并且我们的列表没有理由不成长巨大)。

So what we do here is keep this list of WeakReferences that point to every instance of MyClass that's been created. When you want to obtain a list of them, we iterate through our WeakReferences and snatch out all of them that are alive. Any we find that are no longer alive are placed into another temporary list so that we can delete them from our outer list (so that the WeakReference class itself can be collected and our list doesn't grow huge without reason).

这篇关于在类方法内计算类类型的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆