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>;
また、デリゲート以外の型には変換できないと書かれています。
正しい呼び方は?
/ラッセ
- アップデート *
コードを次のように変更:
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;
}