【发布时间】:2011-07-04 04:00:45
【问题描述】:
我有一个自定义配置部分:
<myServices>
<client clientAbbrev="ABC">
<addressService url="www.somewhere.com" username="abc" password="abc"/>
</client>
<client clientAbbrev="XYZ">
<addressService url="www.somewhereelse.com" username="xyz" password="xyz"/>
</client>
<myServices>
我想将配置称为:
var section = ConfigurationManager.GetSection("myServices") as ServicesConfigurationSection;
var abc = section.Clients["ABC"];
但得到一个
无法对表达式应用索引 'ClientElementCollection' 类型的
我怎样才能做到这一点?
客户端元素集合:
[ConfigurationCollection(typeof(ClientElement), AddItemName = "client")]
public class ClientElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ClientElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ClientElement) element).ClientAbbrev;
}
}
客户端元素:
public class ClientElement : ConfigurationElement
{
[ConfigurationProperty("clientAbbrev", IsRequired = true)]
public string ClientAbbrev
{
get { return (string) this["clientAbbrev"]; }
}
[ConfigurationProperty("addressService")]
public AddressServiceElement AddressService
{
get { return (AddressServiceElement) this["addressService"]; }
}
}
【问题讨论】:
标签: c# configuration configurationsection