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

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

问题描述

如何计算该类的方法中该类类型的对象数量?就此而言,如何在不将对象添加到列表的情况下在类之外执行此操作?

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?

我早该想到的!谢谢!我将暂时不回答它,看看是否有更好的方法,因为我同意.我只是把我的头围绕在 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/.NET 的新手,所以不要让 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 并抓取所有活着的.我们发现不再存在的任何东西都被放入另一个临时列表中,以便我们可以从我们的外部列表中删除它们(以便可以收集 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天全站免登陆