首页 > Windows开发 > 详细

C# list集合数据给Mdoe类属性赋值

时间:2020-03-23 17:10:33      阅读:148      评论:0      收藏:0      [点我收藏+]

1.封装的方法

 public static T GetT<T>(T t, List<string> list)
        {
            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                 properties[i].SetValue(t, list[i]);
            }
            return t;
        }

2.Model类

 public class Staff
    {
        public string id { get; set; }

        public string barcode { get; set; }

        public string name { get; set; }
    }

3.使用实列

 private void button1_Click(object sender, EventArgs e)
        {
            Staff staff = new Staff(); ;
            List<string> list = new List<string>() { "1","2","3"};
            for (int i = 0; i < 3; i++)
            {
                list.Add((i+1).ToString());
            }
            staff = GetT<Staff>(staff, list);

        }

这样看一下staff中已经有值了。

 

C# list集合数据给Mdoe类属性赋值

原文:https://www.cnblogs.com/hanglog/p/12553227.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!