adding expando properties to a typed object at runtime in c#
Asked Answered
C

2

5

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?

Chiao answered 5/3, 2011 at 14:27 Comment(0)
K
13

Have a look at the ConditionalWeakTable<TKey, TValue> Class.

The ConditionalWeakTable<TKey, TValue> class enables language compilers to attach arbitrary properties to managed objects at run time. A ConditionalWeakTable<TKey, TValue> object is a dictionary that binds a managed object, which is represented by a key, to its attached property, which is represented by a value. The object's keys are the individual instances of the TKey class to which the property is attached, and its values are the property values that are assigned to the corresponding objects.

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.


static class ExpandoExtensions
{
    private static readonly ConditionalWeakTable<object, ExpandoObject> props =
        new ConditionalWeakTable<object, ExpandoObject>();

    public static dynamic Props(this object key)
    { 
        return props.GetOrCreateValue(key);       
    } 
}
Kremenchug answered 5/3, 2011 at 14:39 Comment(1)
I must admit I had no idea this existed.Chlo
C
0

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.

Chlo answered 5/3, 2011 at 14:31 Comment(2)
I had a look into this but came away thinking that there must be an easier way! In the example above, we would have to be able to find the ExpandoObject using x, but not actually store x in the props dictionary. To work around that I considered using Dictionary<int, List<dynamic>>, storing things by doing GetHashcode(), and then doing a search through the list for the dynamic which holds a WeakReference to the key object. Not great, and the Expandos still don't get GC'd. I came up with a Dictionary<int, List<dynamic>>Chiao
@mcintyre321: It seems you're in luck, somebody's already implemented your dictionary thing for you.Chlo

© 2022 - 2024 — McMap. All rights reserved.