public IEnumerable <dynamic> GetPageList(string tableName, int categoryId, int pageIndex, int pageSize) { //返回结果集 var returnResult = new List <dynamic>(); //获取数据源 var dataSource = _dataModelService.GetDataSource(tableName, categoryId, pageIndex, pageSize); //反射发出动态创建类型 var customType = DefineDynamicType(tableName, dataSource.Columns); //创建类型 var customTypeFields = customType.GetFields(); //遍历数据源行 for (var rowIndex = 0; rowIndex < dataSource.Rows.Count; rowIndex++) { //实例化对象 var instanceType = Activator.CreateInstance(customType); //遍历对象属性 foreach (var field in customTypeFields) { //遍历对象字段 for (var columnIndex = 0; columnIndex < dataSource.Columns.Count; columnIndex++) { //按对象字段名称完全匹配 if (dataSource.Columns[columnIndex].ColumnName == field.Name) { //给对象字段赋值 field.SetValue(instanceType, dataSource.Rows[rowIndex][dataSource.Columns[columnIndex].ColumnName]); } } } //添加到返回结果集合 returnResult.Add(instanceType); } //返回结果 return(returnResult); }