自定义 app.config 部分,带有一个简单的“添加"列表元素 [英] Custom app.config section with a simple list of "add" elements
问题描述
如何创建一个自定义的 app.config 部分,它只是一个简单的 add
元素列表?
我找到了一些示例(例如 如何在 app.config 中创建自定义配置部分?)像这样:
<公司><公司名称=塔塔汽车"代码=塔塔"/><公司名称=本田汽车"代码=本田"/></公司></注册公司>
但是如何避免额外的集合元素(公司"),使其看起来与 appSettings
和 connectionStrings
部分相同?换句话说,我想:
<add name="Tata Motors" code="Tata"/><add name="Honda Motors" code="Honda"/></registerCompanies>
基于 OP 配置文件的完整示例代码:
<预><代码><配置><configSections><部分名称=注册公司"type="My.MyConfigSection, My.Assembly"/></configSections><注册公司><add name="Tata Motors" code="Tata"/><add name="Honda Motors" code="Honda"/></registerCompanies></配置>这是实现带有折叠集合的自定义配置部分的示例代码
使用 System.Configuration;命名空间我的{公共类 MyConfigSection : ConfigurationSection {[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]公共 MyConfigInstanceCollection 实例 {得到 { 返回 (MyConfigInstanceCollection)this[""];}设置 { this[""] = 值;}}}公共类 MyConfigInstanceCollection : ConfigurationElementCollection {受保护的覆盖 ConfigurationElement CreateNewElement() {返回新的 MyConfigInstanceElement();}受保护的覆盖对象 GetElementKey(ConfigurationElement 元素) {//设置为您要用于键的任何元素属性返回((MyConfigInstanceElement)元素).名称;}}公共类 MyConfigInstanceElement : ConfigurationElement {//确保为上面的 GetElementKey 公开的属性设置 IsKey=true[ConfigurationProperty("name", IsKey = true, IsRequired = true)]公共字符串名称{get { return (string) base["name"];}设置 { 基 [名称"] = 值;}}[ConfigurationProperty("code", IsRequired = true)]公共字符串代码{get { return (string) base["code"];}设置 { 基 [代码"] = 值;}} } }
这是一个如何从代码中访问配置信息的例子.
var config = ConfigurationManager.GetSection("registerCompanies")作为 MyConfigSection;Console.WriteLine(config["Tata Motors"].Code);foreach(config.Instances 中的变量){Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);}
How do I create a custom app.config section that is just a simple list of add
elements?
I have found a few examples (e.g. How to create custom config section in app.config?) for custom sections that look like this:
<RegisterCompanies>
<Companies>
<Company name="Tata Motors" code="Tata"/>
<Company name="Honda Motors" code="Honda"/>
</Companies>
</RegisterCompanies>
But how do I avoid the extra collection element ("Companies") so that it looks the same as the appSettings
and connectionStrings
sections? In other words, I'd like:
<registerCompanies>
<add name="Tata Motors" code="Tata"/>
<add name="Honda Motors" code="Honda"/>
</registerCompanies>
Full example with code based on OP config file:
<configuration>
<configSections>
<section name="registerCompanies"
type="My.MyConfigSection, My.Assembly" />
</configSections>
<registerCompanies>
<add name="Tata Motors" code="Tata"/>
<add name="Honda Motors" code="Honda"/>
</registerCompanies>
</configuration>
Here is the sample code to implement a custom config section with collapsed collection
using System.Configuration;
namespace My {
public class MyConfigSection : ConfigurationSection {
[ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
public MyConfigInstanceCollection Instances {
get { return (MyConfigInstanceCollection)this[""]; }
set { this[""] = value; }
}
}
public class MyConfigInstanceCollection : ConfigurationElementCollection {
protected override ConfigurationElement CreateNewElement() {
return new MyConfigInstanceElement();
}
protected override object GetElementKey(ConfigurationElement element) {
//set to whatever Element Property you want to use for a key
return ((MyConfigInstanceElement)element).Name;
}
}
public class MyConfigInstanceElement : ConfigurationElement {
//Make sure to set IsKey=true for property exposed as the GetElementKey above
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name {
get { return (string) base["name"]; }
set { base["name"] = value; }
}
[ConfigurationProperty("code", IsRequired = true)]
public string Code {
get { return (string) base["code"]; }
set { base["code"] = value; }
} } }
Here is an example of how to access the configuration information from code.
var config = ConfigurationManager.GetSection("registerCompanies")
as MyConfigSection;
Console.WriteLine(config["Tata Motors"].Code);
foreach (var e in config.Instances) {
Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);
}
这篇关于自定义 app.config 部分,带有一个简单的“添加"列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!