gpt4 book ai didi

c# - 从逗号分隔列表生成 HTML 表头

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:11 24 4
gpt4 key购买 nike

有没有办法从逗号分隔列表构建 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);
}

有人知道如何从那个逗号分隔的数组中提取属性名称,并将它们放到一个新行中,我可以将其附加到表中并将其用作标题吗?

可能有更好的方法可以通过反射拉取带有名称的字符串。也许我应该将它们存储在字符串列表或类似的东西中。任何关于如何尽可能清洁地处理它的建议都将非常受欢迎。

最佳答案

您可以使用 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);

关于c# - 从逗号分隔列表生成 HTML 表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36493968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com