问题描述
使用名单,其中,WeakReference的>
将无法工作,因为我想要的。我想要的是在WeakReferences是自动的从列表中删除的时,他们引用的对象被垃圾回收。
Using a List<WeakReference>
will not work as I want. What I want is for WeakReferences to be automatically removed from the list whenever the object they reference is garbage collected.
ConditionalWeakTable&LT; TKEY的,TValue&GT;!
不满足我要么,因为虽然它的键和值是弱引用和收藏价值,不能一一列举。
ConditionalWeakTable<TKey,TValue>
does not satisfy me either, because although its keys and values are weakly referenced and collectable, you cannot enumerate them!
推荐答案
我同意实施 WeakList&LT; T&GT;
是可能的,但我不认为这是完全轻松的。欢迎你来这里使用我的实现。该 WeakCollection&LT; T&GT;
类依赖于<一href="http://nitokitchensink.$c$cplex.com/SourceControl/changeset/view/51391#1006414"><$c$c>WeakReference<T>$c$c>,而这又取决于<一href="http://nitokitchensink.$c$cplex.com/SourceControl/changeset/view/51391#1006416"><$c$c>SafeGCHandle$c$c>.
I agree that implementing a WeakList<T>
is possible, but I don't think it's exactly easy. You're welcome to use my implementation here. The WeakCollection<T>
class depends on WeakReference<T>
, which in turn depends on SafeGCHandle
.
这篇关于有没有办法做到在CLR一个WeakList或WeakCollection(如WeakReference的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!