如何在运行时在expando上添加对象属性?

时间:2012-06-29 04:46:28

标签: c# model-view-controller expandoobject

我读了一篇关于expando对象here的文章,但我希望实现不同的东西 我想在运行时添加带有动态属性的属性对象,将值放在上面然后检索:

    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);
    }  

用法:

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

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

但是我收到了这个错误:

The best overloaded method match for 'System.Runtime.CompilerServices.ConditionalWeakTable<object,System.Dynamic.ExpandoObject>.Add(object, System.Dynamic.ExpandoObject)' has some invalid arguments  
我觉得我误用了这个属性,这有可能实现吗?如果有,怎么样?请帮忙。

修改

这是完整的课程:

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);
        } 
}  

我想要达到的目的是:
我会随机变量,例如“photo_01, photo_12, photo_15, name_01, name_02, age_01, age_02” 如果可能,我想以这种方式使用该方法:

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

然后检索值:

instance.GetDataExtension("photo_" + id)  

3 个答案:

答案 0 :(得分:1)

我没有看到你从ExpandoObject继承,所以你可能需要这样做。 ExpandoObject具有很好的基础结构,可用于创建动态类型。

这是一篇关于创建动态类型的更详细的文章: http://www.codeproject.com/Articles/62839/Adventures-with-C-4-0-dynamic-ExpandoObject-Elasti

答案 1 :(得分:1)

使用conditionalWeakTable和dynamics,您可以在几行中实现动态扩展属性:

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());
    }
}

用法示例在xml注释中:

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");
}

答案 2 :(得分:0)

因为ExpandoObject是密封的,所以不可能。