C#对象和字典 map
//创建一个Person类,类中属性名称和下面字典中的key一致
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
public string Country { get; set; }
public string Grade { get; set; }
public string Tall { get; set; }
}
//创建dictionary
var dic = new Dictionary<string, string>();
dic.Add("Name", "henry");
dic.Add("Age", "18");
dic.Add("Country", "Shanghai");
dic.Add("Grade", "23");
dic.Add("Tall", "180");
var person = new Person();
foreach (var item in dic)
{
//将字典中的key对应到person类中的属性,如果找到对应的属性名称,并为该属性赋与key对应的value
person.GetType().GetProperty(item.Key).SetValue(person, item.Value, null);
}
Console.WriteLine(person.Name);//henry
Console.WriteLine(person.Age);//18
.....