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); } }
反射原理读取对象属性并设置属性值
时间: 2024-10-07 18:50:28