简体   繁体   English

带有列表的用户配置部分

[英]User config section with a list

I know that what I am trying to do is possible, because I have done it - I just no longer have access to the code that I did it in. What is more, I cannot find using Google the details for how to do it. 我知道我正在尝试做的事情是可能的,因为我已经做到了-我再也无法访问在其中执行过的代码。而且,我找不到使用Google的详细信息。

I have a config file containing something like the following: 我有一个包含以下内容的配置文件:

<resources>
    <item title="A" details="aaaaaa" />
    <item title="b" details="bbbbbbb" />
    <item title="c" details="ccccc" />
    <item title="d" details="ddddd" />
</resources>

both the title and the details are (potentially) HTML. 标题和详细信息都是(可能是)HTML。

I want to load these into my code through the handler defined to describe them, which is (basically) 我想通过定义来描述它们的处理程序将它们加载到我的代码中,(基本上)

public class ResourceGroup : ConfigurationSection, IConfigurationSectionHandler
{
    private ResourceGroup() {}
    public static List<ResourceDetail> getDetails()
    {
        var items = new List<ResourceDetail>();
        var entry= ConfigurationManager.GetSection("resources");
???
            items.Add(new ResourceDetail(node));
???            
        return items;
    }

I know that I can look through the entries in this config section in XML, adding resourceDetails entries created form the XML nodes. 我知道我可以浏览XML中此配置部分中的条目,添加从XML节点创建的resourceDetails条目。 However I cannot find how to do it. 但是我找不到如何做。

Alternatively, how do I represent this list of entries in config using the Property definitions. 另外,我如何使用属性定义表示此配置中的条目列表。 I would rather avoid using the "add" approach, because they don't have a key naturally. 我宁愿避免使用“添加”方法,因为它们自然没有密钥。

Thank you. 谢谢。

Edit - config file config section: 编辑-配置文件配置部分:

<configSections>
    <section name="resources" type="BoredWithChurch.ConfigHandlers.ResourceGroup" />
    <section name="links" type="BoredWithChurch.ConfigHandlers.LinksHandler" />
</configSections>

As far as I see, there is no way to let ResourceGroup inherit from ConfigurationSection and ConfigurationElementCollection , which means you'll have to add an additional node that keeps the <item> 's together: 据我所知,没有办法让ResourceGroupConfigurationSectionConfigurationElementCollection继承,这意味着您必须添加一个其他节点,以将<item>保持在一起:

<resources>
    <items>
        <item title="A" details="aaaaaa" />
        <item title="b" details="bbbbbbb" />
        <item title="c" details="ccccc" />
        <item title="d" details="ddddd" />
    </items>
</resources>

Then the ResourceGroup class will look like this: 然后, ResourceGroup类将如下所示:

public class ResourceGroup : ConfigurationSection
{
    [ConfigurationProperty("items", IsRequired=true)]
    public ResourceItemCollection Items
    {
        get { return (ResourceItemCollection)this["items"]; }
    }
}

The ResourceItemCollection class: ResourceItemCollection类:

[ConfigurationCollection(typeof(ResourceItem), AddItemName = "item", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class ResourceItemCollection : ConfigurationElementCollection
{
    public ResourceItem this[int index]
    {
        get { return (ResourceItem)BaseGet(index); }
    }

    public new ResourceItem this[string name]
    {
        get { return (ResourceItem)BaseGet(name); }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ResourceItem();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ResourceItem)element).Title;
    }
}

And last but not least, the ResourceItem class: 最后但并非最不重要的是, ResourceItem类:

public class ResourceItem : ConfigurationElement
{
    [ConfigurationProperty("title", IsRequired = true)]
    public string Title
    {
        get { return (string)this["title"]; }
    }
    [ConfigurationProperty("details", IsRequired = true)]
    public string Details
    {
        get { return (string)this["details"]; }
    }
}

Usage 用法

static void Main(string[] args)
{
    var section = ConfigurationManager.GetSection("resources") as ResourceGroup;
    foreach (ResourceItem item in section.Items)
    {
        Console.WriteLine(item.Title);
    }
    Console.ReadKey();
}

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

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