【问题标题】:Configuration element containing a collection of child elements包含子元素集合的配置元素
【发布时间】:2012-12-01 04:42:34
【问题描述】:

我想在我的 web.config 中有一个自定义部分,如下所示:

<MyMainSection attributeForMainSection = "value foo">

    <add name = "foo" 
    type = "The.System.Type.Of.Foo, Assembly, Qualified Name Type Name" />

    <add name = "bar" 
    type = "The.System.Type.Of.Bar, Assembly, Qualified Name Type Name" />

</MyMainSection>

我已经定义了以下代码:

using System.Configuration;

class MyMainSection : ConfigurationSection
{
    /*I've provided custom implemenation. 
      Not including it here for the sake of brevity. */ 
    [ConfigurationProperty("attributeForMainSection")]
    public string AttributeForMyMainSection { get; set; }

    [ConfigurationProperty("add")]
    public AddElement TheAddElement { get; set; }

    private class AddElement: ConfigurationElement
    {
        /* Implementation done */
    }

}

如果我想允许多个添加元素,这个属性 TheAddElement 应该是 IEnumerable&lt;AddElement&gt; 还是只是 AddElement

【问题讨论】:

  • 你真的应该重命名你的问题,根据你的实际问题,这很容易产生误导。

标签: c# configuration web-config app-config


【解决方案1】:

你也不会引入一个新的ConfigurationCollectionElement,例如

部分

class MyMainSection : ConfigurationSection
{
    [ConfigurationProperty("", IsRequired=true, IsDefaultCollection=true)]
    public AddElementCollection Instances 
    {
        get { return (AddElementCollection) this[""]; }
        set { this[""] = value; }
    }
}

收藏

public class AddElementCollection : ConfigurationElementCollection 
{
    protected override ConfigurationElement CreateNewElement() 
    {
        return new AddElement();
    }

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

元素

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

【讨论】:

  • 谢谢你,詹姆斯。还有两个问题:(1) 您没有为存储 AddElementsCollection 的字典条目提供键。这是故意的吗?我可以在那里拥有任何名称的钥匙还是必须将其留空? (2) 您已将属性命名为 Instances。这是强制性的还是我可以更改该名称?
  • 嗯,实际上还有 三个 问题:P 回答您关于密钥的问题 - 您在此处使用的密钥是您在 ConfigurationProperty 属性中输入的任何值,我离开了地雷空白,所以我必须将索引器属性留空。 “我可以在那里有任何名称的键还是我必须将其留空?” - 您可以使用任何您喜欢的键名称,只要它与 ConfigurationProperty 值匹配即可。 “您已将属性命名为 Instances。这是强制性的还是我可以更改该名称” - 不是强制性的,仅用于说明目的。
【解决方案2】:

使用ConfigurationElementCollection 处理ConfigurationElement 对象的集合。实现此类以将自定义 ConfigurationElement 元素的集合添加到 ConfigurationSection:

// Define a custom section that contains a custom 
// UrlsCollection collection of custom UrlConfigElement elements. 
// This class shows how to use the ConfigurationCollectionAttribute. 
public class UrlsSection : ConfigurationSection
{
    // Declare the Urls collection property using the 
    // ConfigurationCollectionAttribute. 
    // This allows to build a nested section that contains 
    // a collection of elements.
    [ConfigurationProperty("urls", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(UrlsCollection),
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public UrlsCollection Urls
    {
        get
        {
            UrlsCollection urlsCollection =
                (UrlsCollection)base["urls"];
            return urlsCollection;
        }
    }

}

// Define the custom UrlsCollection that contains the  
// custom UrlsConfigElement elements. 
public class UrlsCollection : ConfigurationElementCollection
{
  // ...

http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    相关资源
    最近更新 更多