我创建了一个自定义配置部分,可以将尽可能多的 XML 行添加到我的自定义部分并循环并打印它们。这很好用。
<eTMSoftware>
<Program Id="1" Customer="SomeCust" Type="DC" InstalledLocation="C:\Program Files (x86)\eMenuDesktopComponent 1.1.1.1_Customer" LogBaseDestination="C:\_eTM Logging"/>
<Program Id="2" Customer="ThisCustNew" Type="DC" InstalledLocation="Some_Path" LogBaseDestination="DEST"/>
<Program Id="3" Customer="AnotherNewCust" Type="DC" InstalledLocation="Some_Another_Path" LogBaseDestination="DEST"/>
</eTMSoftware>
我遵循了关于配置自定义配置的指南,并ConfigurationElementCollection
为我的ConfigurationSection
.
我的最终目标:循环遍历ConfigurationElementCollection
(其中包含上面的 3 个 XML 节点)并将所有Customer
属性添加到字符串数组中。
我无法弄清楚如何做到这一点,因为即使ConfigurationElementCollection
派生自ICollection
and IEnumerable
,我也无权访问Select()
orWhere()
方法。
任何人都可以提供解决方案吗?
如果需要,我可以提供代码。我以为一开始放在这里太多了。
编辑:这是我尝试投射的两种不同方式
public void VerifyShareDestinationsPerCustomer(eTMProgamsElementCollection configuredItems)
{
string[] customersFromConfig = configuredItems.Cast<eTMProgramElement>()
.Select(p => p.Customer);
}
错误:
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“string[]”。存在显式转换(您是否缺少演员表?)。
public void VerifyShareDestinationsPerCustomer(eTMProgamsElementCollection configuredItems)
{
string[] customersFromConfig = configuredItems.Cast<object>()
.Select(p => p.Customer);
}
错误:
对象不包含“客户”的定义,并且找不到接受“对象”类型的第一个参数的可访问扩展方法“客户”。
找到答案:我能够将ToArray<string>()
方法添加到数组定义的末尾,并且它可以与 Haukinger 的代码一起使用!谢谢!
string[] customersFromConfig = configuredItems.Cast<eTMProgramElement>()
.Select(p => p.Customer)
.ToArray<string>();