反射原理读取对象属性并设置属性值

 Dictionary<string, string> dicNodes = new Dictionary<string, string>();
       foreach (XmlNode node in nodes.ChildNodes)
       {
           if (node.NodeType==XmlNodeType.Element)
           {
             
              dicNodes.Add(node.Attributes["name"].Value,node.Attributes["value"].Value);
           }
       }
        tb_b_employee Employee = new tb_b_employee();

        Type type = Employee.GetType();
       //取得属性集合
       PropertyInfo[] pi = type.GetProperties();
       foreach (PropertyInfo item in pi)
       {
           //给属性赋值
           if (dicNodes[item.Name] != null)
           {
               item.SetValue(Employee,dicNodes[item.Name], null);
           }
       }

  

原文地址:https://www.cnblogs.com/liang-ling/p/3910449.html