In one of the projects I take part in there is a vast use of WeakAction
. That\'s a class that allows to keep reference to an action instance without causing its tar
You want to extend the lifetime of the closure class instance to be exactly the same that the A
instance has. The CLR has a special GC handle type for that: the Ephemeron, implemented as internal struct DependentHandle
.
ConditionalWeakTable
class. You could create one such table per WeakAction
with exactly one item in it. The key would be an instance of A
, the value would be the closure class instance.DependentHandle
using private reflection.ConditionalWeakTable
instance. It probably requires synchronization to use. Look at the docs.Consider opening a connect issue to make DependentHandle
public and link to this question to provide a use case.