[英]Are there Weak References in .NET?
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? 你能在.NET中创建弱引用吗?
For reference: 以供参考:
Answer From MSDN: 从MSDN回答:
To establish a weak reference with an object, you create a WeakReference using the instance of the object to be tracked. 要使用对象建立弱引用,可以使用要跟踪的对象的实例创建WeakReference。 You then set the Target property to that object and set the object to null. 然后,将Target属性设置为该对象,并将该对象设置为null。 For a code example, see WeakReference in the class library. 有关代码示例,请参阅类库中的WeakReference。
Yes, there's a generic weak reference class. 是的,有一个通用的弱引用类。
Can you create weak references in .NET? 你能在.NET中创建弱引用吗?
Yes: 是:
WeakReference r = new WeakReference(obj);
Uses System.WeakReference
. 使用System.WeakReference
。
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/ 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). 您创建了两个新的Person
对象(我刚刚为此示例创建的简单对象,包含Name
属性和一些参考跟踪代码)。 Next you set the member variables to the newly created instances of the Person
objects. 接下来,将成员变量设置为新创建的Person
对象实例。
_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. 区别在于你会注意到_strongRef
只是一个常规的正常引用,而_weakRef
被设置为一个WeakReference
对象,其中person对象(p1)
作为构造函数中的参数传入。
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. 然后,应该对_weakRef
成员变量持有的p1
目标对象进行垃圾回收。 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: 如果WeakReference
仍处于活动状态,您可以使用以下代码将WeakReference
转换为强引用或普通引用:
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. 现在, p
引用被视为强引用,在不再使用之前不会被收集。 If you wanted to keep the reference around after the scope you could set that to a member variable. 如果您想在范围之后保留引用,可以将其设置为成员变量。
Here is the full (non thread safe) implementation sample of WeakReference
这是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. WeakReference
位于System
命名空间中,因此无需为其包含任何特殊程序集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.