zoukankan      html  css  js  c++  java
  • C# list集合数据给Mdoe类属性赋值

    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中已经有值了。

  • 相关阅读:
    知识加油站
    markdown 使用
    今日计划
    继续继续
    我回来了
    2020/5/8
    2020/5/10
    明天一定要学习了
    入驻博客园
    this is a test
  • 原文地址:https://www.cnblogs.com/hanglog/p/12553227.html
Copyright © 2011-2022 走看看