I want to read all the values of each row
internal class ExcelTable<T> : IExcelTable where T : IExcelTableRow
{
void Foo(){
var props = typeof(T).GetProperties().ToList();
for (int r = 0; r < Rows.Count; r++)
{
IExcelTableRow row = Rows[r];
foreach (var p in props)
{
// Throws error on the first prop = {Int32 Id}
var v = p.GetValue(row);
}
}
}
}
But it throws the following error
System.Reflection.TargetException: 'Object does not match target type.'
The type of T
is IExcelTableRow
and the implementation is ExcelTranscationLogRow
.
It throws an error on the first row which has the following values
internal class ExcelTranscationLogRow : IExcelTableRow
{
public int Id { get; set; } // 3619
public TransactionType TransactionType { get; set; }// TransactionType.Execution
public string Identifier { get; set; } // "Placeholder"
public TransactionStatus Status { get; set; } // LogType.Success
public LogType LogType { get; set; } // TransactionStatus.Trace
public string Log { get; set; } // "Getting KOT and CPR"
public DateTime TimeStamp { get; set; } // {05-07-2022 19:06:38}
}
It works fine for other implementations of IExcelTableRow
e.g.
internal class ExcelTransactionRow : IExcelTableRow
{
public int Id { get; set; }
public string Identifier { get; set; }
public TransactionStatus Status { get; set; }
public DateTime StartTime { get; set; }
public DateTime? EndTime { get; set; }
}
How are you instantiating ExcelTable<T>
? If each row can be of a different type, then you will need to move the assignment of props
inside the row loop and assign for each row.
Untested example:
internal class ExcelTable : IExcelTable
{
void Foo()
{
for (int r = 0; r < Rows.Count; r++)
{
IExcelTableRow row = Rows[r];
var props = row.GetType().GetProperties().ToList();
foreach (var p in props)
{
// Throws error on the first prop = {Int32 Id}
var v = p.GetValue(row);
}
}
}
}