有没有办法从逗号分隔列表构建HTML表头?
我使用反射从一个类中获取属性名称。在此之前,我正在定义要从列表中排除的属性。
var toExclude = new HashSet<string>
{
"ID",
"SubSystemID",
"System",
"Component",
"ReferenceNumber",
"IsValid"
};
var propsIndividuals = typeof(Person).GetProperties(BindingFlags.Public | BindingFlags.Instance).
Where(property => !toExclude.Contains(property.Name));
var header = string.Join(", ", propsIndividuals.Select(p => p.Name.ToString()).ToArray());
对于表格内容,我使用的是HtmTableRow:
foreach (var invalidIndividual in mailMessageObject.InvalidIndividuals)
{
rowIndividuals = new HtmlTableRow();
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.FirstName});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Surname});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.MiddleName});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.DateOfBirth});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Gender});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Address1});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Address2});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Address3});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Address4});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.City});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.PostalCode});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.Country});
rowIndividuals.Cells.Add(new HtmlTableCell {InnerText = invalidIndividual.NationalID});
tableIndividuals.Rows.Add(rowIndividuals);
}
任何人都知道如何从该逗号分隔数组中提取属性名称,并将它们放到一个新行中,我可以将其附加到表并将其用作标题?
可能有一种更好的方法来使用带反射的名称来拉取字符串。也许我应该将它们存储在字符串列表或类似的东西中。 任何关于如何处理这个问题的建议都会非常受欢迎。
答案 0 :(得分:1)
您可以使用MSDN中的示例。
更新您的代码段:
var toExclude = new HashSet<string>
{
"ID",
"SubSystemID",
"System",
"Component",
"ReferenceNumber",
"IsValid"
};
var propsIndividuals = typeof(Person).GetProperties(BindingFlags.Public | BindingFlags.Instance).
Where(property => !toExclude.Contains(property.Name));
var rowHeader = new new HtmlTableRow("th")
foreach (var property in propsIndividuals)
{
rowHeader.Cells.Add(new HtmlTableCell { InnerText = property.Name });
}
tableIndividuals.Rows.Add(rowHeader);