自定义 ConfigurationSection

13

我使用IConfigurationSectionHandler接口获取有关我的自定义配置节的信息。但是它已被弃用,我想改用ConfigurationSection。

如何以这种方式创建自定义ConfigurationSection并使用ConfigurationSection代替IConfigurationSectionHandler:

<CustomSectionBlaBla>
   <Parent name="DB">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
    ...
   <Parent name="UI">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
<CustomSectionBlaBla/>

你应该查看Jon Rista在CodeProject上关于.NET 2.0配置的三个系列文章。 - 揭开.NET 2.0配置的神秘面纱 - 解码.NET 2.0配置的神秘之处 - 破解.NET 2.0配置的谜团 非常值得推荐,写作精良且极其有帮助!它应该可以逐步指导您如何达到所需的结果。其他 - marc_s
1个回答

22

这是我创建的一个配置部分的示例。应该能指导你朝着正确的方向前进。

public class ImportConfiguration : ConfigurationSection
{
    [ConfigurationProperty("importMap")]
    public ImportMapElementCollection ImportMap
    {
        get
        {
            return this["importMap"] as ImportMapElementCollection;
        }
    }
}

public class ImportColumnMapElement : ConfigurationElement
{
    [ConfigurationProperty("localName", IsRequired = true, IsKey = true)]
    public string LocalName
    {
        get
        {
            return this["localName"] as string;
        }
        set
        {
            this["localName"] = value;
        }
    }

    [ConfigurationProperty("sourceName", IsRequired = true)]
    public string SourceName
    {
        get
        {
            return this["sourceName"] as string;
        }
        set
        {
            this["sourceName"] = value;
        }
    }
}

public class ImportMapElementCollection : ConfigurationElementCollection
{
    public ImportColumnMapElement this[object key]
    {
        get
        {
            return base.BaseGet(key) as ImportColumnMapElement;
        }
    }

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

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

    protected override bool IsElementName(string elementName)
    {
        bool isName = false;
        if (!String.IsNullOrEmpty(elementName))
            isName = elementName.Equals("columnMap");
        return isName;
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ImportColumnMapElement)element).LocalName;
    }
}

这里展示了它在web.config中的使用:

<importConfiguration>
    <importMap>
        <columnMap localName="PropertyID" sourceName="Detail Number"/>
        <columnMap localName="DateOfOpen" sourceName="Open Date &amp; Time"/>
        <columnMap localName="StartTime" sourceName="Open Date &amp; Time"/>
        <columnMap localName="ClosingTime" sourceName="Close Date &amp; Time"/>
        <columnMap localName="StreetAddress" sourceName="Street Address"/>
    </importMap>
</importConfiguration>

3
为了完整性,实现代码如下:ImportConfiguration columns = (ImportConfiguration)ConfigurationManager.GetSection("importConfiguration"); foreach (ImportColumnMapElement col in columns.ImportMap) { Debug.Write(col.localName + col.sourceName); }说明:该代码读取名为"importConfiguration"的配置部分,并迭代其中的"ImportMap"元素列表。对于每个"ImportMap"元素,将其本地名称和源名称一起输出到调试窗口。 - amackay11

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接