python映射实体类_C#、Python中分别是怎么实现通过字符串获取实体类的值以及给实体类赋值...

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; }

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值