gpt4 book ai didi

c# - 自定义配置部分

转载 作者:可可西里 更新时间:2023-11-01 08:01:45 24 4
gpt4 key购买 nike

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

如何使用此 View 创建自定义 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/>

最佳答案

这是我创建的配置部分的示例。应该为您指明正确的方向。

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>

关于c# - 自定义配置部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983127/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com