首页 > 解决方案 > 需要向 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)或类似的事情。有人有什么想法吗?

标签: c#arraysxmlforeachaxis2

解决方案


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()
     }
 };

推荐阅读