2
我已經延長IDictionary的是這樣的:Howto調用方法擴展IDictionary(反射)?
public static T ToClass<T>(this IDictionary<string, string> source) where T : class, new()
{
T someObject = new T();
foreach (KeyValuePair<string, string> item in source)
{
someObject.GetType().GetProperty(item.Key).SetValue(someObject, item.Value, null);
}
return someObject;
}
而且我使用的方法有問題,試着這樣說:
TestClass test = _rep.Test().ToClass<TestClass>;
而且它說,它不能轉換到非委託類型。
調用它的正確方法是什麼?
/拉塞
- UPDATE *
更改代碼:
public static T ToClass<T>(this IDictionary<string, string> source) where T : class, new()
{
Type type = typeof(T);
T ret = new T();
foreach (var keyValue in source)
{
type.GetProperty(keyValue.Key).SetValue(ret, keyValue.Value, null);
}
return ret;
}
非常感謝喬治,聖誕快樂:) – 2011-12-22 08:37:30