我当前正在设置类对象 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)];
}