0
我有一個自定義配置部分:C#自定義配置部分
<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"]; }
}
}
謝謝。我確實發現,如果我在ClientElement中爲ClientAbbrev屬性設置ConfigurationProperty的「IsKey」屬性,我的索引器可能爲: – 2011-02-25 09:19:39
public new ClientElement this [string clientAbbreviation] { get {return(ClientElement)BaseGet(clientAbbreviation); } } – 2011-02-25 09:20:16