c# - 需要向 NewActionConfiguration 添加参数,但不能在定义中使用 foreach
问题描述
使用 Axis Communications VAPIX WSDL API - 我正在设置一个NewActionConfiguration
,它采用我保存在列表中的一些参数,但是 API 文档的实现方式我无法在定义对象时遍历我的参数列表 XMLnewAction
对象。
//This is how the API docs say to do it:
NewActionConfiguration newAction = new NewActionConfiguration
{
TemplateToken = overlayToken,
Name = "Overlay Text",
Parameters = new ActionParameters
{
Parameter = new[]
{
new ActionParameter { Name = "text", Value = "Trigger:Active" },
new ActionParameter { Name = "channels", Value = "1" },
new ActionParameter { Name = "duration", Value = "15" }
}
}
};
//This is what I need to do:
NewActionConfiguration newAction = new NewActionConfiguration
{
Name = xmlPrimaryAction["Name"].InnerText,
TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
Parameters = new[]
{
foreach (ActionParameter actionParameter in actionParameterList)
{
new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value };
}
}
};
API不允许我只做一个:newAction.Parameters.Parameter.Add(actionParameter)
或类似的事情。有人有什么想法吗?
解决方案
Found it! Thanks for the help @Vitor you were close but learned how to cast my list as my object after i found this: Convert List to object[]
Here's what finally worked:
var actionParameterList = new List<ActionParameter>();
foreach (XmlNode xmlParameter in xmlActionParameters)
{
ActionParameter actionParameter = new ActionParameter();
actionParameter.Name = xmlParameter["Name"].InnerText;
actionParameter.Value = xmlParameter["Value"].InnerText;
actionParameterList.Add(new ActionParameter { Name = actionParameter.Name, Value = actionParameter.Value });
}
NewActionConfiguration newAction = new NewActionConfiguration
{
Name = xmlPrimaryAction["Name"].InnerText,
TemplateToken = xmlPrimaryAction["ActionTemplate"].InnerText,
Parameters = new ActionParameters
{
Parameter = actionParameterList.Cast<ActionParameter>().ToArray()
}
};
推荐阅读
- matlab - 记不清。为您的选项键入“帮助记忆”
- django - 打开 EDX - LMS:无法发送验证电子邮件
- jquery - 如何在 Spring Boot 应用程序中使用 jQuery 进行 AJAX 调用后刷新 div 标签?
- php - Laravel 5.8 PDF 上传
- node.js - 快速请求第二次触发
- reactjs - React + Mobx:不触发渲染
- c++ - C++ 中的未定义行为
- c# - 如何使用 Xamarin.Forms.iOS 在渲染器视图中更改 UIPickerView 文本颜色?
- elasticsearch - 合并两个聚合的结果
- c# - 创建结构化数据库并将其绑定到 ListView C#