1) 创建一个类并继承自ConfigurationSection
public class ComponentCustomSection : ConfigurationSection
2) 添加属性并使用 ConfigurationProperty 属性对其进行标记
[ConfigurationProperty("name")]
public string Name
{
get { return ((string)this["name"]); }
}
[ConfigurationProperty("title")]
public string Title
{
get { return ((string)this["title"]); }
}
[ConfigurationProperty("description")]
public string Description
{
get { return ((string)this["description"]); }
}
3) 在配置文件中添加自定义配置部分的信息(在配置标签下)
<configSections>
<section name="component" type="YourAssembly.ComponentCustomSection, YourAssembly"/>
</configSections>
4)您可以使用以下代码获取该部分
var section = ConfigurationManager.GetSection("component")
请注意,这适用于单个组件标记。
如果你需要有N个标签,你应该把它们封装在一个父标签中,像这样
<components>
<component name="name1" title="title1" description="description1"/>
<component name="name2" title="title2" description="description2"/>
<component name="name3" title="title3" description="description3"/>
</components>
Here 是一篇很好的文章,可帮助您开始自定义配置部分
Here 是一个关于有一个带有子元素的 customSection 应该可以帮助你的问题