2

我在这里阅读了一些关于 expando 对象的文章,但我想实现不同的目标。
我想在运行时添加具有动态属性的属性对象,为其赋值,然后稍后检索:

    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)  
4

3 回答 3

1

我没有看到您从 ExpandoObject 继承,因此您可能需要这样做。ExpandoObject 有很好的基础设施,您可以使用它来创建您的动态类型。

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

于 2012-06-29T06:46:22.463 回答
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");
}
于 2017-04-18T09:06:48.020 回答
0

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

于 2012-10-02T10:07:58.437 回答