1

I have implemented custom config section like this

 <directorySettings>
   <domainConfigurationList>
     <domainConfiguration id="1" name="name1" url="http://www.google.com"/>
     <domainConfiguration id="2" name="name2" url="http://www.bing.com"/>
   </domainConfigurationList>
 </directorySettings>

I can set only one field as key:

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DirectorySettingsConfigurationElement)element).Name;
    }

but I also need to have possibility to search using not only by key 'name' but also by field 'id'. Here is part of my DirectorySettingsConfigurationElementCollection:

    public DirectorySettingsConfigurationElement Get(string name)
    {
        return (DirectorySettingsConfigurationElement)BaseGet(name);
    }

    public DirectorySettingsConfigurationElement GetById(int id)
    {
        // ???
        return null;
    }

What is the best way to get my DirectorySettingsConfigurationElement by custom field (not by key)

4

1 回答 1

0

可能有更直接的答案;但我通常看到的方法是使用ConfigurationElementCollectionhttp://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx

您将获得 domainConfigurations 的集合,并且您可以遍历它或使用 LINQ 查询来获取匹配值。

于 2012-12-27T14:17:59.963 回答