我目前正在设置类对象的所有值Record
。
这是我现在用来逐个属性填充记录的代码。
// Loop through each field in the result set
for (int i = 0; i <= resultItems.Length; i++)
{
Record newRecord = new Record()
{
itemtype = resultItems[i - (fieldCount - 0)],
itemdesc = resultItems[i - (fieldCount - 1)],
prodcode = resultItems[i - (fieldCount - 2)],
proddesc = resultItems[i - (fieldCount - 3)],
curstat = resultItems[i - (fieldCount -4)],
totfree = resultItems[i - (fieldCount -5)],
totphys = resultItems[i - (fieldCount -6)],
pcolgroup = resultItems[i - (fieldCount -7)],
scolgroup = resultItems[i - (fieldCount -8)],
totpo = resultItems[i - (fieldCount - 9)],
totso = resultItems[i - (fieldCount - 10)],
quality = resultItems[i - (fieldCount - 11)],
statusdesc = resultItems[i - (fieldCount - 12)],
groupcode = resultItems[i - (fieldCount - 13)],
qualitydes = resultItems[i - (fieldCount - 14)],
pcoldesc = resultItems[i - (fieldCount - 15)],
scoldesc = resultItems[i - (fieldCount - 16)],
pgroupdesc = resultItems[i - (fieldCount - 17)],
};
}
是否可以在不对所有属性名称进行硬编码的情况下动态地遍历每个属性?
像这样:
// Create new Record instance
Record newRecord = new Record();
for (int e = 0; e < propertyCount.Length - 1; e++)
{
newRecord[fieldname] = resultItems[i - (fieldCount - e)];
}
1
您是否尝试过反射?stackoverflow.com/questions/997747/…–
—
kol
—
谜州
您能解释一下在什么地方以及如何保持resultItems数组中的属性与索引之间的关系吗?
—
Erno