我有一個IList只讀屬性的類。我創建了一個簡單的擴展方法AddCSV,將多個項目添加到該列表中。我想創建一個動作委託來通過擴展方法填充列表。到目前爲止,我有CreateDelegate在擴展方法
private Action<TListPropertyContainer, TDataValue> CreateListPropertySetter<TListPropertyContainer, TDataValue>(string listName)
{
var list = typeof(TListPropertyContainer).GetProperty(listName);
var method = typeof(Extensions).GetMethod("AddCSV");
return (Action<TListPropertyContainer, TDataValue>)Delegate.CreateDelegate(typeof(Action<TListPropertyContainer, TDataValue>), list, method);
}
但顯然這是行不通的!
我知道還有其他的選擇。例如 一)我可以繼承列表到我自己的客戶類,並添加AddCSV有 B)我可以做的項目屬性讀/寫,並設置一個完全填充的名單到類
我會很感激,如果有人可以糾正我。
許多THX
西蒙
許多thx。你是對的,我已經過於複雜了!你的代碼示例有訣竅。 – 2011-05-13 10:27:26