C # list data set assigned to a class attribute Mdoe

1. The packaging method

 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 class

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

        public string barcode { get; set; }

        public string name { get; set; }
    }

3. Use real column

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

        }

So look at the staff have been worth it.

 

Guess you like

Origin www.cnblogs.com/hanglog/p/12553227.html
Recommended