如何创建自定义app.config部分,它只是一个简单的add元素列表?
我找到了一些示例(例如,如何在app.config中创建自定义配置部分?),以查看如下所示的自定义部分:
<RegisterCompanies>
<Companies>
<Company name="Tata Motors" code="Tata"/>
<Company name="Honda Motors" code="Honda"/>
</Companies>
</RegisterCompanies>
Run Code Online (Sandbox Code Playgroud)
但是,我如何避免额外的集合元素("公司"),使其看起来与appSettings和connectionStrings部分相同?换句话说,我想:
<registerCompanies>
<add name="Tata Motors" code="Tata"/>
<add name="Honda Motors" code="Honda"/>
</registerCompanies>
Run Code Online (Sandbox Code Playgroud)
Jay*_*ker 109
基于OP配置文件的代码的完整示例:
<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>
Run Code Online (Sandbox Code Playgroud)
以下是使用折叠集合实现自定义配置节的示例代码
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; }
} } }
Run Code Online (Sandbox Code Playgroud)
以下是如何从代码访问配置信息的示例.
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);
}
Run Code Online (Sandbox Code Playgroud)
JJS*_*JJS 32
无需自定义配置部分.
App.Config中
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="YourAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<!-- value attribute is optional. omit if you just want a list of 'keys' -->
<YourAppSettings>
<add key="one" value="1" />
<add key="two" value="2"/>
<add key="three" value="3"/>
<add key="duplicate" value="aa"/>
<add key="duplicate" value="bb"/>
</YourAppSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
取回
// This casts to a NameValueCollection because the section is defined as a
/// AppSettingsSection in the configSections.
NameValueCollection settingCollection =
(NameValueCollection)ConfigurationManager.GetSection("YourAppSettings");
var items = settingCollection.Count;
Debug.Assert(items == 4); // no duplicates... the last one wins.
Debug.Assert(settingCollection["duplicate"] == "bb");
// Just keys as per original question? done... use em.
string[] allKeys = settingCollection.AllKeys;
// maybe you did want key/value pairs. This is flexible to accommodate both.
foreach (string key in allKeys)
{
Console.WriteLine(key + " : " + settingCollection[key]);
}
Run Code Online (Sandbox Code Playgroud)
SwD*_*n81 19
基于Jay Walker的上述答案,这是一个完整的工作示例,增加了索引编制的能力:
<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>
Run Code Online (Sandbox Code Playgroud)
以下是使用折叠集合实现自定义配置节的示例代码
using System.Configuration;
using System.Linq;
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 new MyConfigInstanceElement this[string elementName]
{
get
{
return this.OfType<MyConfigInstanceElement>().FirstOrDefault(item => item.Name == elementName);
}
}
}
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; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是如何从代码访问配置信息的示例.
MyConfigSection config =
ConfigurationManager.GetSection("registerCompanies") as MyConfigSection;
Console.WriteLine(config.Instances["Honda Motors"].Code);
foreach (MyConfigInstanceElement e in config.Instances)
{
Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);
}
Run Code Online (Sandbox Code Playgroud)
小智 8
根据Jay Walker的回答,访问元素需要通过迭代"Instances"集合来完成.即.
var config = ConfigurationManager.GetSection("registerCompanies")
as MyConfigSection;
foreach (MyConfigInstanceElement e in config.Instances) {
Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);
}
Run Code Online (Sandbox Code Playgroud)