C#中如何向自定义配置元素添加键值项列表?
给自定义ConfigurationElement添加键值项列表的实现方案
要在你的Node配置元素中添加键值项列表,我们需要借助.NET配置系统中的ConfigurationElementCollection类来实现集合类型的配置属性。下面是完整的实现步骤:
1. 定义单个键值对的配置元素
首先创建一个用来封装单个键值项的自定义ConfigurationElement,比如命名为KeyValueItem:
public class KeyValueItem : ConfigurationElement { [ConfigurationProperty("key", IsRequired = true, IsKey = true)] public string Key { get => (string)this["key"]; set => this["key"] = value; } [ConfigurationProperty("value", IsRequired = true)] public string Value { get => (string)this["value"]; set => this["value"] = value; } }
这里把key设为唯一标识(IsKey=true),确保集合中不会出现重复键的项。
2. 定义键值项的集合管理类
接下来创建继承自ConfigurationElementCollection的集合类,用来管理多个KeyValueItem:
public class KeyValueItemCollection : ConfigurationElementCollection { // 重写创建新元素的方法,返回我们自定义的键值项 protected override ConfigurationElement CreateNewElement() { return new KeyValueItem(); } // 重写获取元素唯一键的方法,返回键值项的Key属性 protected override object GetElementKey(ConfigurationElement element) { return ((KeyValueItem)element).Key; } // 提供两种索引器,方便通过键名或下标访问集合项 public KeyValueItem this[string key] { get => (KeyValueItem)BaseGet(key); } public KeyValueItem this[int index] { get => (KeyValueItem)BaseGet(index); set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } }
3. 修改原有Node类,添加集合属性
把这个集合属性添加到你原来的Node类中:
public class Node : ConfigurationElement { public Node() { } [ConfigurationProperty("name", IsRequired=true, IsKey=true, DefaultValue="")] public string Name { get => (string)this["name"]; set => this["name"] = value; } [ConfigurationProperty("kind", IsRequired=false)] public string Kind { get => (string)this["kind"]; set => this["kind"] = value; } // 添加键值项集合属性,指定集合类型为KeyValueItemCollection [ConfigurationProperty("keyValues", IsDefaultCollection = false)] public KeyValueItemCollection KeyValues { get => (KeyValueItemCollection)this["keyValues"]; set => this["keyValues"] = value; } }
IsDefaultCollection=false表示这个集合需要在配置文件中显式声明节点名称,不会自动隐式处理。
4. 配置文件中的写法示例
在App.config中可以这样配置包含键值列表的Node元素:
<configuration> <configSections> <!-- 注册你的自定义配置节(假设Node属于某个配置节下的元素) --> <section name="customNodesSection" type="YourNamespace.CustomNodesSection, YourAssemblyName" /> </configSections> <customNodesSection> <nodes> <node name="PaymentNode" kind="Online"> <keyValues> <add key="ApiKey" value="abc123xyz" /> <add key="Timeout" value="60" /> <add key="RetryCount" value="3" /> </keyValues> </node> </nodes> </customNodesSection> </configuration>
(注:如果需要管理多个Node元素,你还需要定义对应的ConfigurationSection和ConfigurationElementCollection来承载所有Node,核心的键值集合逻辑不受影响。)
5. 代码中读取键值项的示例
在C#代码中读取这些键值项的方式很直观:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var nodesSection = (CustomNodesSection)config.GetSection("customNodesSection"); var targetNode = nodesSection.Nodes["PaymentNode"]; // 遍历所有键值项 foreach (KeyValueItem item in targetNode.KeyValues) { Console.WriteLine($"配置项:{item.Key} = {item.Value}"); } // 通过键名直接获取对应值 string apiKey = targetNode.KeyValues["ApiKey"].Value;
这样就完美实现了给自定义配置元素添加键值项列表的需求!
内容的提问来源于stack exchange,提问作者Some User
相关产品推荐
相关产品推荐

