.NET 中是否存在弱引用?

发布于 2024-07-10 02:15:16 字数 478 浏览 5 评论 0原文

我想在我的应用程序中保留某一类对象的列表。 但我仍然希望该对象被垃圾收集。 您可以在 .NET 中创建弱引用吗?

供参考:

来自 MSDN 的回答:

建立一个弱引用 对象,您创建一个 WeakReference 使用对象的实例 被跟踪。 然后您设置目标 属性到该对象并设置 对象为空。 对于代码示例, 请参阅类中的 WeakReference 图书馆。

I would like to keep a list of a certain class of objects in my application. But I still want the object to be garbage collected. Can you create weak references in .NET?

For reference:

Answer From MSDN:

To establish a weak reference with an
object, you create a WeakReference
using the instance of the object to be
tracked. You then set the Target
property to that object and set the
object to null. For a code example,
see WeakReference in the class
library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯了 2024-07-17 02:15:16

你能在.NET中创建弱引用吗?

是:

WeakReference r = new WeakReference(obj);

使用 System.WeakReference

Can you create weak references in .NET?

Yes:

WeakReference r = new WeakReference(obj);

Uses System.WeakReference.

海夕 2024-07-17 02:15:16

是的,有一个通用的弱引用类。

MSDN > 弱引用

Yes, there's a generic weak reference class.

MSDN > Weak Reference

随遇而安 2024-07-17 02:15:16

是的...

这里有一个很好的例子:

http://web.archive.org/web/20080212232542/http://www.robherbst.com/blog/2006/08/21/c- weakreference-example/

在你的类中,你创建了两个成员变量:

WeakReference _weakRef = null;

Person _strongRef = null;

你创建了两个新的 Person 对象(它们是我刚刚为本示例创建的简单对象,由 Name< /code> 属性和一些参考跟踪代码)。 接下来,将成员变量设置为新创建的 Person 对象实例。

_strongRef = p;

_weakRef = new WeakReference(p1);

这里的区别是,_strongRef 只是一个常规的普通引用,而 _weakRef 被设置为带有 person 对象的 WeakReference 对象< code>(p1) 作为构造函数中的参数传入。

如果要发生垃圾收集,或者只是为了测试目的,您自己调用它:

GC.Collect();

那么由 _weakRef 成员变量保存的 p1 目标对象应该被垃圾收集。 您可以编写代码来检查:

if (_weakRef.IsAlive)

如果 WeakReference 仍然存在,您可以使用如下代码将 WeakReference 转换为强引用或普通引用:

Person p = _weakRef.Target as Person;

现在 p 引用被视为强引用,在不再使用之前不会被收集。 如果您想在范围之后保留引用,您可以将其设置为成员变量。

Yes...

There is a pretty good example to be found here:

http://web.archive.org/web/20080212232542/http://www.robherbst.com/blog/2006/08/21/c-weakreference-example/

In your class you created two member variables:

WeakReference _weakRef = null;

Person _strongRef = null;

You created two new Person objects (which are simple objects I just created for this example, consisting of a Name property and some reference tracking code). Next you set the member variables to the newly created instances of the Person objects.

_strongRef = p;

_weakRef = new WeakReference(p1);

The difference here you’ll notice that _strongRef is just a regular normal reference, whereas _weakRef is set to a WeakReference object with the person object (p1) passed in as a parameter in the constructor.

If a garbage collection were to occur, or just for testing purposes you called it yourself with:

GC.Collect();

Then the p1 target object that is held by the _weakRef member variable should be garbage collected. You can write code to check:

if (_weakRef.IsAlive)

If the WeakReference is still alive you can convert the WeakReference to a strong or normal reference by using code like this:

Person p = _weakRef.Target as Person;

Now the p reference is treated as a strong reference and won’t be collected until it is no longer used. If you wanted to keep the reference around after the scope you could set that to a member variable.

水晶透心 2024-07-17 02:15:16

这是 WeakReference 的完整(非线程安全)实现示例

ClassA objA = new ClassA();
WeakReference wr = new WeakReference(objA);
// do stuff 
GC.Collect();
ClassA objA2;
if (wr.IsAlive)
    objA2 = wr.Target as ClassA; 
else
    objA2 = new ClassA(); // create it directly if required

WeakReference 位于 System 命名空间中,因此无需为其包含任何特殊程序集。

Here is the full (non thread safe) implementation sample of WeakReference

ClassA objA = new ClassA();
WeakReference wr = new WeakReference(objA);
// do stuff 
GC.Collect();
ClassA objA2;
if (wr.IsAlive)
    objA2 = wr.Target as ClassA; 
else
    objA2 = new ClassA(); // create it directly if required

WeakReference is in the System namespace hence no need to include any special assembly for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文