根據這個:Adding properties and methods to an ExpandoObject, dynamically!,
...您可以使用在Expando對象作爲你的價值持有人,並將其轉換爲一個IDictionary <字符串對象>當您要添加動態命名屬性。
例
dynamic myobject = new ExpandoObject();
IDictionary<string, object> myUnderlyingObject = myobject;
myUnderlyingObject.Add("IsDynamic", true); // Adding dynamically named property
Console.WriteLine(myobject.IsDynamic); // Accessing the property the usual way
釷經過測試並在控制檯屏幕上打印出「真實」。
當然,在你的情況下,你的底層對象必須從另一個類繼承,這個例子只是給你一個潛在的自定義實現的想法。
可能在您的類實現中包含expando對象,並將調用重定向到您的類中的expando對象的實例tryget和tryset?
UPDATE
如果你的基類派生DynamicObject(這意味着你可以覆蓋所有TrySet/GET /調用方法),那麼,你也可以使用一個字典內。在嘗試獲取/設置覆蓋時,您可以執行任何需要的事件觸發,並將設置委託給內部字典。
要添加新屬性(或刪除現有屬性),您可以重寫TryInvoke。例如,當方法名稱爲「AddProperty」並且存在一個字符串類型的參數時,則可以使用參數的名稱在字典中添加一個新項目。同樣,你會動態定義一個「RemoveProperty」等等,你甚至不需要一個expando對象。
class MyBaseClass: DynamicObject
{
// usefull functionality
}
class MyClass: MyBaseClass
{
Dictionary<string, object> dynamicProperties = new Dictionary<string, object>();
override bool TryGetMember(...)
{
// read the value of the requested property from the dictionary
// fire any events and return
}
override bool TrySetMember(...)
{
// set the value of the requested property to the dictionary
// if the property does not exist,
// add it to the dictionary (compile time dynamic property naming)
// fire any events
}
override bool TryInvoke(...)
{
// check what method is requested to be invoked
// is it "AddProperty"??
// if yes, check if the first argument is a string
// if yes, add a new property to the dictionary
// with the name given in the first argument (runtime dynamic property naming)
// if there is also a second argument of type object,
// set the new property's value to that object.
// if the method to be invoked is "RemoveProperty"
// and the first argument is a string,
// remove from the Dictionary the property
// with the name given in the first argument.
// fire any events
}
}
// USAGE
static class Program
{
public static void Main()
{
dynamic myObject = new MyClass();
myObject.FirstName = "John"; // compile time naming - TrySetMember
Console.WriteLine(myObject.FirstName); // TryGetMember
myObject.AddProperty("Salary"); // runtime naming (try invoke "AddProperty" with argument "Salary")
myObject.Salary = 35000m;
Console.WriteLine(myObject.Salary); // TryGetMember
myObject.AddProperty("DateOfBirth", new DateTime(1980,23,11)); // runtime naming (try invoke "AddProperty" with fisrt argument "DateOfBirth" and second argument the desired value)
Console.WriteLine(myObject.DateOfBirth); // TryGetMember
myObject.RemoveProperty("FirstName"); // runtime naming (try invoke "RemoveProperty" with argument "FirstName")
Console.WriteLine(myObject.FirstName); // Should print out empty string (or throw, depending on the desired bahavior) because the "FirstName" property has been removed from the internal dictionary.
}
}
當然,正如我所說的,只有當您的基類從DynamicObject派生時纔有效。
你還是不明白爲什麼動態和ExpandoObject進入.NET。原因與你想要的完全相反。 ExpandoObject仍然使用Dictionary,以及它是Dictionary。編譯器僅將所有「屬性」調用轉換爲Dictionary [NameOfProperty]。這使得代碼更好。但這就是它唯一的代碼糖。即使您使用任何動態輸入法,您也可以使用字典進行操作。 –
Euphoric
2010-10-14 14:41:03
如何設置字典項目時需要提出的事件? – sbenderli 2010-10-14 14:43:35
[動態添加成員到動態對象](http:// stackoverflow。com/questions/2079870/dynamic-adding-members-to-a-dynamic-object) – nawfal 2014-07-20 07:04:11