Is it possible to create a truely weak-keyed dictionary in C#?

前端 未结 1 1698
误落风尘
误落风尘 2020-12-09 16:17

I\'m trying to nut out the details for a true WeakKeyedDictionary<,> for C#... but I\'m running into difficulties.

I realise this is a non-trivial

1条回答
  •  北海茫月
    2020-12-09 16:24

    Have a look at the ConditionalWeakTable Class.

    Enables compilers to dynamically attach object fields to managed objects.

    It's essentially a dictionary where both the key and the value are a WeakReference, and the value is kept alive as long as the key is alive.

    Note! This class does not use GetHashCode and Equals to do equality comparisons, it uses ReferenceEquals.

    0 讨论(0)
提交回复
热议问题