usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication1
{classProgram
{static void Main(string[] args)
{#region 通过字符串设置实体类的值
//初始化一个实体类//Student model_stu = new Student();//string id_str = "stu_id";//string name_str = "stu_name";//string addr_str = "stu_address";//Type type = model_stu.GetType();//获取类型//PropertyInfo property_info_id = type.GetProperty(id_str);//PropertyInfo property_info_name = type.GetProperty(name_str);//PropertyInfo property_info_addr = type.GetProperty(addr_str);//property_info_id.SetValue(model_stu, 5);//property_info_name.SetValue(model_stu, "李四");//property_info_addr.SetValue(model_stu, "北京市");//Console.WriteLine(model_stu.stu_id);//Console.WriteLine(model_stu.stu_name);//Console.WriteLine(model_stu.stu_address);//Console.ReadKey();
#endregion
#region 通过字符串获取实体类的值
//初始化一个实体类
Student model_stu = newStudent()
{
stu_id= 1,
stu_name= "张三",
stu_address= "上海市"};string id_str = "stu_id";string name_str = "stu_name";string addr_str = "stu_address";
Type type= model_stu.GetType();//获取类型
PropertyInfo property_info_id =type.GetProperty(id_str);
PropertyInfo property_info_name=type.GetProperty(name_str);
PropertyInfo property_info_addr=type.GetProperty(addr_str);
Console.WriteLine(property_info_id.GetValue(model_stu));
Console.WriteLine(property_info_name.GetValue(model_stu));
Console.WriteLine(property_info_addr.GetValue(model_stu));
Console.ReadKey();#endregion}
}public classStudent
{public int stu_id { get; set; }public string stu_name { get; set; }public string stu_address { get; set; }
}
}