I have this code. I receive several lists as parameters that I insert into a list of objects.
public static void SaveTransactionsToSeparateFiles(List<Receivables> genRecList,
List<Dilution> genDilList, List<Accountable> genAccList, string excelFile,
string typeReport, List<Outstanding> genOutList = null)
{
List<object> listObj = new List<object>
{
genRecList,
genDilList,
genAccList,
genOutList
};
}
Then I have a foreach where I run through all the lists in List(object) listObj
, turns them into datatables and adds the content of each lists in each datatable.
foreach (var obj in listObj)
{
DataTable genTable = HelperMethods.ConvertListToDataTable(obj, true);
Worksheet pantaReiWorkSheet = pantaReiWorkSheets[sheet.SheetName];
int iRow = 0;
foreach (DataRow row in genTable.Rows)
{
iRow++;
int iCol = 0;
foreach (DataColumn col in genTable.Columns)
{
iCol++;
pantaReiWorkSheet.Cells[iRow + 1, iCol] = row[col.ColumnName];
}
}
i++;
}
Then I have the convert to datatable method.
public static DataTable ConvertListToDataTable(object genList, bool toExcel)
{
DataTable genDt = new DataTable();
PropertyInfo[] p = p.genList.GetType().GetProperties();
for (int prop = 1; prop < p.Length - 2; prop++)
{
genDt.Columns.Add(new DataColumn(p[prop].Name, p[prop].PropertyType));
}
foreach (object t in (IEnumerable<object>)genList)
{
DataRow genDR = genDt.NewRow();
for (int prop = 1; prop < p.Length - 2; prop++)
{
genDR[p[prop].Name] = p[prop].GetValue(t, null);
}
genDt.Rows.Add(genDR);
}
return genDt;
}
The thing is, I'm not geting the properties of each list inside the List listObj.
So, how can I access the List<Receivables> genRecList
, List<Dilution> genDilList
,
List<Accountable> genAccList
, List<Outstanding> genOutList
properties??
I had this done before, but with List(T)
and one list at a time and it worked. Now it's a mess.
If you want to get
Type
of classT
inList<T>
:So your code should be changed: