robertono,
Словарь в класс достаточно красиво преобразуется при помощи рефлексии, но следует учесть что не создать объекты которые не имеют конструктора без параметров
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| TrackInfo trackInfo = DictToObject<TrackInfo>(info);
private T DictToObject<T>(Dictionary<string, object> dict)
{
Type type = typeof (T);
var obj = Activator.CreateInstance(type);
foreach (var o in dict)
{
type.GetProperty(o.Key).SetValue(obj, o.Value);
}
return (T) obj;
}
public class TrackInfo
{
public int aid { get; set; }
public int owner_id { get; set; }
public string artist { get; set; }
public string title { get; set; }
public int duration { get; set; }
public string url { get; set; }
public int genre { get; set; }
} |
|
|