Question

I read some article regarding expando object here but I want to achieve different thing.
I want to add property object with dynamic property at runtime, put value on it and then retrieve later:

    private static readonly ConditionalWeakTable<object, ExpandoObject> props = new ConditionalWeakTable<object, ExpandoObject>();
    public static void AddDataExtension(this object key, dynamic value)
    {
        props.Add(key, value);
    }

    public static dynamic GetDataExtension(this object key)
    {
        ExpandoObject ex = null;
        return props.TryGetValue(key, out ex);
    }  

Usage:

'Insert data at runtime'
instance.AddDataExtension("Hello", "hi");

'Get the data at runtime'
instance.GetDataExtension("Hello")  

But I receive this error:

The best overloaded method match for 'System.Runtime.CompilerServices.ConditionalWeakTable<object,System.Dynamic.ExpandoObject>.Add(object, System.Dynamic.ExpandoObject)' has some invalid arguments  

I think I misused this property, is this possible to achieve? if yes, how? Please help.

Edit

here is the complete class:

public static class instance
{
    private static readonly ConditionalWeakTable<object, ExpandoObject> props = new ConditionalWeakTable<object, ExpandoObject>();
        public static void AddDataExtension(this object key, dynamic value)
        {
            props.Add(key, value);
        }

        public static dynamic GetDataExtension(this object key)
        {
            ExpandoObject ex = null;
            return props.TryGetValue(key, out ex);
        } 
}  

What I want to achieve is this:
I will have random varialbes, for example, "photo_01, photo_12, photo_15, name_01, name_02, age_01, age_02"
If possible I want to use the method in this way:

id = <fetch from dbase>
instance.AddDataExtension("photo_" + id, byte[]);  

And then retrieve the value:

instance.GetDataExtension("photo_" + id)  
Was it helpful?

Solution 3

It is not possible because ExpandoObject is sealed.

OTHER TIPS

I don't see you inheriting from ExpandoObject so you'll probably need to do that. ExpandoObject has nice infrastructure that you can use to create your dynamic type.

Here's an article that goes in more detail about creating dynamic types: http://www.codeproject.com/Articles/62839/Adventures-with-C-4-0-dynamic-ExpandoObject-Elasti

Using the conditionalWeakTable and dynamics, you could implement dynamic extension properties in a few lines:

using System.Dynamic;
using System.Runtime.CompilerServices;

namespace ExtensionProperties
{
    /// <summary>
    /// Dynamically associates properies to a random object instance
    /// </summary>
    /// <example>
    /// var jan = new Person("Jan");
    ///
    /// jan.Age = 24; // regular property of the person object;
    /// jan.DynamicProperties().NumberOfDrinkingBuddies = 27; // not originally scoped to the person object;
    ///
    /// if (jan.Age &lt; jan.DynamicProperties().NumberOfDrinkingBuddies = 27)
    /// Console.WriteLine("Jan drinks too much");
    /// </example>
    /// <remarks>
    /// If you get 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' you should reference Microsoft.CSharp
    /// </remarks>
    public static class ObjectExtensions
    {
        ///<summary>Stores extended data for objects</summary>
        private static ConditionalWeakTable<object, object> extendedData = new ConditionalWeakTable<object, object>();

        /// <summary>
        /// Gets a dynamic collection of properties associated with an object instance,
        /// with a lifetime scoped to the lifetime of the object
        /// </summary>
        /// <param name="obj">The object the properties are associated with</param>
        /// <returns>A dynamic collection of properties associated with an object instance.</returns>
        public static dynamic DynamicProperties(this object obj) => extendedData.GetValue(obj, _ => new ExpandoObject());
    }
}

A usage example is in the xml comments:

var jan = new Person("Jan");

jan.Age = 24; // regular property of the person object;
jan.DynamicProperties().NumberOfDrinkingBuddies = 27; // not originally scoped to the person object;

if (jan.Age && jan.DynamicProperties().NumberOfDrinkingBuddies == 27)
{
    Console.WriteLine("Jan drinks too much");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top