Hey there,
I have a class defiend as follows:public class VisitorStat {
public int? PageViews { get; set; }
public int? Visits { get; set; }
public int? Visitors { get; set; } ...
}
At some point in my program, I need to gather the Visitor stats and create a report table.List<VisitorStat> visitorStats = GetVisitorStats(startDate, endDate);
My issue is this: Because there are many rows in the table, rather than hard coding the name for each row as: "PageViews", "Visits", "Visitors"... I want a way to use the name of the fields in the class to create the Row name and insert the appropriate values.
I found out about System.Reflection and wrote the following:
Type type = typeof(VisitorStat); // Get type pointer
FieldInfo[] fields = type.GetFields(); // Obtain all fields
//Start creating a row for each field in the class
foreach (var field in fields) // Loop through fields
{
object temp = field.GetValue(null);
//The first cell in the Row contains the name of the field
Cell Col2Row1 = new Cell(new Paragraph(field.Name));
PdfTable.AddCell(Col2Row1);
//for item returned when calling the function to get t
View Complete Post