简体   繁体   English

自定义ConfigurationSection

[英]custom ConfigurationSection

I use IConfigurationSectionHandler interface to get information about my custom config section. 我使用IConfigurationSectionHandler接口来获取有关我的自定义配置部分的信息。 But it's deprecated and I want to use ConfigurationSection instead. 但是不推荐使用,我想改用ConfigurationSection。

How to create custom ConfigurationSection with this view and using ConfigurationSection instead IConfigurationSectionHandler: 如何使用此视图创建自定义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/>

Here's an example of a configuration section that I created. 这是我创建的配置部分的示例。 Should point you in the right direction. 应该为您指明正确的方向。

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;
    }
}

And here it is being used in the web.config: 在这里,它正在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>

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject. 您应该在CodeProject上查看Jon Rista的有关.NET 2.0配置的三部分系列。

Highly recommended, well written and extremely helpful! 强烈推荐,写得很好,非常有帮助!

It should show you how to get to your desired result - step by step. 它应该向您展示如何逐步达到所需的结果。

The other item to check out is the Configuration Section Designer - a Visual Studio add-in that will allow you to visually design your config sections, and have CSD create all the necessary classes for you - highly recommended! 另一个要检查的项目是配置节设计器 -一个Visual Studio加载项,它使您可以直观地设计配置节,并让CSD为您创建所有必需的类-强烈建议!

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

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