2012-06-29 59 views
2

我讀了一些關於expando對象here的文章,但我想實現不同的事情。
我想在運行時動態屬性添加屬性對象,把它的值,然後在以後檢索:如何在運行時在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); 
     } 
} 

我想實現的是:
我會隨機varialbes,例如, 「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) 
+0

你可以添加更多的信息你問題? 「實例」的類型是什麼?你的'AddDataExtension'有兩個參數,但你用3來調用它:'instance.AddDataExtension(「Hello」,「hi」)'參數是:'instance','「Hello」','「hi」'同樣是對於'GetDataExtension'爲true。目前還不清楚你試圖達到什麼目標。 – nemesv

+0

請參閱我的編輯。希望我能解釋清楚。謝謝 – fiberOptics

+1

您在字典中添加的內容不是ExpandoObject - 這就是爲什麼您會收到錯誤。從你的例子看來,你看起來並不需要一個ExpandoObject - 你只需要任何類型的對象(即System.Object)。 – Bond

回答

0

因爲ExpandoObject是密封的,是不可能的。

1

使用conditionalWeakTable和動態,你可以在幾行實現動態擴展屬性:

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"); 
} 
相關問題