自定义ConfigurationSection

Tim*_*aev 13 .net c# configuration configurationsection

我使用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/>
Run Code Online (Sandbox Code Playgroud)

Jam*_*son 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里它被用在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>
Run Code Online (Sandbox Code Playgroud)

  • 为了完整性,实现:`ImportConfiguration columns =(ImportConfiguration)ConfigurationManager.GetSection("importConfiguration"); foreach(columns.ImportMap中的ImportColumnMapElement col){Debug.Write(col.localName + col.sourceName); }` (3认同)

mar*_*c_s 13

您应该在CodeProject上查看Jon Rista关于.NET 2.0配置的三部分系列.

强烈推荐,写得很好,非常有帮助!

它应该告诉你如何达到你想要的结果 - 一步一步.

另一个要检查的项目是Configuration Section Designer - 一个Visual Studio加载项,它允许您可视化地设计您的配置部分,并让CSD为您创建所有必需的类 - 强烈推荐!