Is there any way in .net to bind a dictionary of properties to an instance at runtime, i.e., as if the base object class had a property like:
public IDictionary Items { get; }
I have come up with a solution involving a static dictionary and extension method
void Main()
{
var x = new object();
x.Props().y = "hello";
}
static class ExpandoExtension {
static IDictionary<object, dynamic> props = new Dictionary<object, dynamic>();
public static dynamic Props(this object key)
{
dynamic o;
if (!props.TryGetValue(key, out o)){
o = new ExpandoObject();
props[key] = o;
}
return o;
}
}
but this stops the objects from getting GC'd as the the props collection holds a reference. In fact, this is just about ok for my particular use case, as I can clear the props down manually once I've finished with the particular thing I'm using them for, but I wonder, is there some cunning way to tie the ExpandoObject to the key while allowing garbage collection?
You could use a WeakReference to reference the objects so that they can still be garbage collected. You'll still have to clean up your dictionary by hand though, as the objects themselves are destroyed.
Have a look at the ConditionalWeakTable<TKey, TValue> Class.
Essentially it's a dictionary where both the keys and the values are weakly referenced, and a value is kept alive as long as the key is alive.