带有configSource属性的.NET自定义配置集合

时间:2014-04-16 12:21:00

标签: c# .net configuration

问题

我有一个带有嵌套配置集合的自定义配置部分。我已将主要自定义配置部分移动到其自己的文件中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="my.section" type="My.App.CustomSection, My.App" />
  </configSections>
  <my.section configSource="app\custom.config" />
</configuration

app\custom.config

<?xml version="1.0" encoding="utf-8"?>
<my.section>
  <children>
    <child configSource="children\child1.config" />
  </children>
</my.section>

我的孩子在app\children\child1.config提交了文件:

<?xml version="1.0" encoding="utf-8"?>
<child name="myChild">
  ...
</child>

app\custom.config中,<children>元素实现为ConfigurationElementCollection,如果子元素是内联的,则可以正常工作,但我看到了带有消息的异常&# 34; 无法识别的属性&#39; configSource&#39;。请注意,属性名称区分大小写。&#34;在custom.config文件中的<child configSource="children\child1.config" />行。

问题

是否可以对配置集合的子元素中的元素使用configSource属性?如果是这样,我可能在这里做错了什么?

目前,我会使用内联儿童 - 但是在我们的情况下能够使用自定义配置来源会非常有用,所以任何答案都会非常感激。

备注

注意1:如果我的路径错误,我还尝试制作相对于根web.config的路径,甚至将所有配置移动到根文件夹 - 这对错误没有影响。< / p>

注意2:我已阅读this question并确保我符合接受(且唯一)答案中指定的条件 - 我的配置文件正在bin文件夹中输出,并且它们都具有相应的{{1声明。

更新:

根据Jonesy的评论中的要求,ConfigurationSection代码:

<?xml version="1.0" encoding="utf-8" ?>

public class CustomSection : ConfigurationSection
{
    private static readonly ConfigurationPropertyCollection properties;
    private static readonly ConfigurationProperty children;

    static CustomSection()
    {
        children = new ConfigurationProperty( 
            "children", 
            typeof( ChildSectionCollection ), 
            null, 
            ConfigurationPropertyOptions.IsRequired
        );

        properties = new ConfigurationPropertyCollection
        {
            children
        };
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return properties; }
    }

    [ConfigurationProperty( "children", IsRequired = true )]
    public ChildSectionCollection ChildrenSection
    {
        get { return ( ChildSectionCollection ) base[ children ]; }
    }
}

public class ChildSection : ConfigurationSection
{
    private static readonly ConfigurationPropertyCollection properties;
    private static readonly ConfigurationProperty name;

    static ChildSection()
    {
        name = new ConfigurationProperty( 
            "name", 
            typeof( string ), 
            string.Empty, 
            ConfigurationPropertyOptions.IsRequired
        );

        properties = new ConfigurationPropertyCollection
        {
            name
        };
    }

    [ConfigurationProperty( "name", IsRequired = true )]
    public string Name
    {
        get { return ( string ) base[ name ]; }
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return properties; }
    }
}

,最后:

[ConfigurationCollection( typeof( ChildSection ), CollectionType = ConfigurationElementCollectionType.BasicMap, AddItemName = "child" )]
public class ChildSectionCollection : ConfigurationElementCollection
{
    private static readonly ConfigurationPropertyCollection properties;

    static ChildSectionCollection()
    {
        properties = new ConfigurationPropertyCollection();
    }

    protected override ConfigurationPropertyCollection Properties
    {
        get { return properties; }
    }

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

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

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMap; }
    }

    protected override string ElementName
    {
        get { return "child"; }
    }
}

注3: 我的代码设计基于Jon Rista的CodeProject文章Unraveling the Mysteries of .NET 2.0 Configuration

0 个答案:

没有答案