0

カスタム構成セクションがあります。

  <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"]; }
    }
}
4

1 に答える 1

1

インデクサーを追加する必要がありますClientElementCollection

何かのようなもの

public ClientElement this[string key]
{
     get
     {
           return this.Cast<ClientElement>()
               .Single(ce=>ce.ClientAbbrev == key);
     }
}
于 2011-02-25T08:53:25.197 に答える