我是 C# 新手。这是我开始工作的硬编码:
InputProperty grantNumber = new InputProperty();
grantNumber.Name = "udf:Grant Number";
grantNumber.Val = "571-1238";
Update update = new Update();
update.Items = new InputProperty[] { grantNumber };
现在我想推广它以支持 Update 对象中无限数量的项目,我想出了这个但它无法编译:
Update update = BuildMetaData(nvc); //call function to build Update object
以及这里的函数本身:
private Update BuildMetaData(NameValueCollection nvPairs)
{
Update update = new Update();
InputProperty[] metaData; // declare array of InputProperty objects
int i = 0;
foreach (string key in nvPairs.Keys)
{
metaData[i] = new InputProperty(); // compiler complains on this line
metaData[i].Name = "udf:" + key;
foreach (string value in nvPairs.GetValues(key))
metaData[i].Val = value;
}
update.Items = metaData;
return update; // return the Update object
}