我试图建立一个将所有的IEnumerable转换为对象的通用方法[,]。这样做的目的是通过ExcelDNA插入excel,理想情况下需要2d对象数组。转换IEnumerable的<T>反对[,] C#
我是新来思考,需要一些严重的帮助,以填补空白这里。 代码贴在下面是我到目前为止,我所需要的将是获得T的propertie在外环的DataSource的索引i。在内部循环中依次获取每个属性的值并插入到对象[,]中。
任何帮助表示赞赏。 感谢 理查德
public object[,] ConvertListToObject<T>(IEnumerable<T> DataSource)
{
int rows = DataSource.Count();
//Get array of properties of the type T
PropertyInfo[] propertyInfos;
propertyInfos = typeof(T).GetProperties(BindingFlags.Public);
int cols = propertyInfos.Count(); //Cols for array is the number of public properties
//Create object array with rows/cols
object[,] excelarray = new object[rows, cols];
for (int i = 0; i < rows; i++) //Outer loop
{
for(int j = 0; j < cols; j++) //Inner loop
{
object[i,j] = //Need to insert each property val into j index
}
}
return excelarray;
}
}
对于文档'GetValue'说,第二个参数为空非索引属性。这将减少垃圾收集器的负担。 – phoog
是啊,谢谢,我只是从你的回答中偷走了:) – Rawling
够公平的,我从你的回答中偷了几个修复程序:-) – phoog