如何实现支持多Exchange节点的App.Config自定义嵌套配置节
实现多Exchange节点配置的修改方案
第一步:修复ExchangeFeatures类的属性赋值错误
原代码中属性的set方法逻辑错误,需修正为正确的赋值方式:
namespace AppWithConfigs { public class ExchangeFeatures : ConfigurationElement { [ConfigurationProperty("Name", IsRequired = true)] public string Name { get { return (string)this["Name"]; } set { this["Name"] = value; } } [ConfigurationProperty("ApiKey", IsRequired = true)] public string ApiKey { get { return (string)this["ApiKey"]; } set { this["ApiKey"] = value; } } [ConfigurationProperty("ApiSecret", IsRequired = true)] public string ApiSecret { get { return (string)this["ApiSecret"]; } set { this["ApiSecret"] = value; } } [ConfigurationProperty("ApiPassPhrase", IsRequired = false)] public string ApiPassPhrase { get { return (string)this["ApiPassPhrase"]; } set { this["ApiPassPhrase"] = value; } } } }
第二步:创建配置元素集合类
新增继承自ConfigurationElementCollection的类,用于管理多个ExchangeFeatures节点:
public class ExchangeElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new ExchangeFeatures(); } protected override object GetElementKey(ConfigurationElement element) { return ((ExchangeFeatures)element).Name; } public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMap; protected override string ElementName => "Exchange"; public ExchangeFeatures this[int index] { get { return (ExchangeFeatures)BaseGet(index); } set { if (BaseGet(index) != null) BaseRemoveAt(index); BaseAdd(index, value); } } public new ExchangeFeatures this[string name] { get { return (ExchangeFeatures)BaseGet(name); } } }
第三步:修改ExchangeSetting配置节类
将原来单个ExchangeFeatures属性替换为集合属性:
public class ExchangeSetting : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public ExchangeElementCollection Exchanges { get { return (ExchangeElementCollection)this[""]; } } }
第四步:更新App.config配置文件
现在可以直接添加多个Exchange节点:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="ExchangeSetting" type="AppWithConfigs.ExchangeSetting, AppWithConfigs"/> </configSections> <ExchangeSetting> <Exchange Name="Binance" ApiKey="B-Key" ApiSecret="B-Secret" ApiPassPhrase="B-Phrase" /> <Exchange Name="Coinbase" ApiKey="C-Key" ApiSecret="C-Secret" ApiPassPhrase="C-Phrase" /> <!-- 可继续添加更多Exchange节点 --> </ExchangeSetting> </configuration>
第五步:修改Program类遍历所有配置
更新Main方法,遍历输出所有交易所配置:
public class Program { static void Main(string[] args) { var exchangeSetting = ConfigurationManager.GetSection("ExchangeSetting") as ExchangeSetting; if (exchangeSetting != null && exchangeSetting.Exchanges.Count > 0) { foreach (ExchangeFeatures exchange in exchangeSetting.Exchanges) { Console.WriteLine($"===== 交易所:{exchange.Name} ====="); Console.WriteLine($"ApiKey = {exchange.ApiKey}"); Console.WriteLine($"ApiSecret = {exchange.ApiSecret}"); Console.WriteLine($"ApiPassPhrase = {exchange.ApiPassPhrase ?? "未设置"}"); Console.WriteLine(); } } else { Console.WriteLine("未找到任何交易所配置"); } Console.ReadKey(); } }
修改完成后,程序即可支持多个Exchange节点,正常遍历获取所有配置信息。
内容的提问来源于stack exchange,提问作者Zaken
相关产品推荐
相关产品推荐

