IList list; //取得List对象里面的所有属性 PropertyInfo[] propertys = list[0].GetType().GetProperties(); //遍历属性 foreach (PropertyInfo pi in propertys) { result.Columns.Add(pi.Name, pi.PropertyType); } //遍历List for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); //遍历属性 foreach (PropertyInfo pi in propertys) { //遍历类里面的属性..并取出 object obj = pi.GetValue(list[i], null); tempList.Add(obj); //也可以根据属性名...取属性值 //list[i].GetType().GetProperty(fieldName); //Object objValue = pi.GetValue(line, null); } }
.NET 反射机制
最新推荐文章于 2024-01-11 17:26:33 发布