Attach and use properties to objects at run-time which are automatically collected after the host object is collected.
This is accomplished with weak-references, implemented in the ConditionalWeakTable class
.NET support | NuGet package |
---|---|
>= Framework 4.0 || >= Standard 2.0 |
var foo = new Foo();
// Attach some things to `foo`
foo.Fmrn().Set("Name", "Value");
foo.Fmrn().Set("Bar", new Bar());
// Retrieve them
var bar = foo.Fmrn().Get<Bar>("Bar");
var name = foo.Fmrn().Get<String>("Name");
// Retrieve with `TryGet`
var didGetUnsetProperty = foo.Fmrn().TryGet("Nope", out var unset);