繁体   English   中英

如何在ConfigurationSection类型的集合中进行搜索?

[英]How do I search within a collection of type ConfigurationSection?

如果我有一个ConfigurationSection类型的集合, 我如何在集合中搜索?

(我是C#noob和业余爱好者)

我有这门课:

(来自http://net.tutsplus.com/tutorials/asp-net/how-to-add-custom-configuration-settings-for-your-asp-net-application/

public class FeedRetrieverSection : ConfigurationSection  
{  
  [ConfigurationProperty("feeds", IsDefaultCollection =   
  public FeedElementCollection Feeds
    {  
      get { return (FeedElementCollection)this["feeds"]; }  
      set { this["feeds"] = value; }  
    }  
}

我看到如何使用基于_Config声明的“for each”迭代它:

public static FeedRetrieverSection _Config =  
        ConfigurationManager.GetSection("feedRetriever") as FeedRetrieverSection; 

我无法弄清楚:如何在集合中搜索给定的名称?

使用_Config的声明,如上所示,我想使用linq或字典从这个<feeds>列表中获取单个“记录”?

全栈:

Web配置中有这个:

<feedRetriever>  
    <feeds>  
        <add name="Nettuts+" url="http://feeds.feedburner.com/nettuts" cache="false"/>  
        <add name="Jeremy McPeak" url="http://www.wdonline.com/feeds/blog/rss/" />  
        <add name="Nicholas C. Zakas" url="http://feeds.nczonline.net/blog/" />  
    </feeds>  
</feedRetriever>  

以这种方式代码表示:

public class FeedElement : ConfigurationElement  
{  
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]  
    public string Name  
    {  
        get { return (string)this["name"]; }  
        set { this["name"] = value; }  
    }  

    // etc for all of the elements...
}

它包含在ConfigurationElementCollection中,如下所示:

[ConfigurationCollection(typeof(FeedElement))]  
public class FeedElementCollection : ConfigurationElementCollection  
{  
    protected override ConfigurationElement CreateNewElement()  
    {  
        return new FeedElement();  
    }  

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

FeedElementCollection是一个非泛型集合,它将包含FeedElement 要在其上使用LINQ,您需要使用OfType <>Cast<>方法使其“通用”。 然后你可以做过滤:

_Config.Feeds.OfType<FeedElement>().Where(e => e.Name == "Jeremy McPeak");

暂无
暂无

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

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