简体   繁体   English

自定义配置部分键

[英]Custom config section key

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'. 但是我还需要有可能不仅使用键“名称”而且还使用字段“ id”进行搜索。 Here is part of my DirectorySettingsConfigurationElementCollection : 这是我的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) 按自定义字段(而不按键)获取DirectorySettingsConfigurationElement的最佳方法是什么

There might be a more direct answer; 可能会有更直接的答案; but the way I've generally seen this done is by using a ConfigurationElementCollection ( http://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollection.aspx ) 但是我通常认为完成此操作的方式是使用ConfigurationElementCollectionhttp://msdn.microsoft.com/zh-cn/library/system.configuration.configurationelementcollection.aspx

You'd get a collection of domainConfigurations and you'd be able to iterate across it or use a LINQ query to get the matching value. 您将获得domainConfigurations的集合,并且可以对其进行迭代或使用LINQ查询来获取匹配的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM