.NET 4 and later only
Good news, everyone!
The perfect tool for this job is built in .NET 4 and it’s called ConditionalWeakTable<TKey, TValue>
. This class:
- can be used to associate arbitrary data with managed object instances much like a dictionary (although it is not a dictionary)
- does not depend on memory addresses, so is immune to the GC compacting the heap
- does not keep objects alive just because they have been entered as keys into the table, so it can be used without making every object in your process live forever
- uses reference equality to determine object identity; moveover, class authors cannot modify this behavior so it can be used consistently on objects of any type
- can be populated on the fly, so does not require that you inject code inside object constructors