c# - 从 C# 中的配置文件读取保持顺序的键值对
问题描述
我将配置存储在 app.config 文件中,如下所示:
<configSections>
<sectionGroup name="FieldMappingSettings">
<section name="RequiredFields" type="System.Configuration.DictionarySectionHandler"/>
</sectionGroup>
</configSections>
<FieldMappingSettings>
<RequiredFields>
<add key="ColumnName 1" value="Config Value1" />
<add key="ColumnName 2" value="Config Value2" />
<add key="ColumnName 3" value="Config Value3" />
<add key="ColumnName 4" value="Config Value4" />
<add key="ColumnName 5" value="Config Value5" />
</RequiredFields>
</FieldMappingSettings>
我正在阅读这些键值对使用
var requiredFields = (ConfigurationManager.GetSection("FieldMappingSettings/RequiredFields") as System.Collections.Hashtable)
.Cast<System.Collections.DictionaryEntry>()
.ToDictionary(n => n.Key.ToString(), n => n.Value.ToString());
我需要以相同的顺序使用此配置进行进一步处理,但与System.Collections.Hashtable
用于强制转换一样,这些键值不按配置顺序存储(Hashtable
使用散列键存储它们)。
所以问题是:
既然返回类型ConfigurationManager.GetSection()
是object
,我们可以把它转换成Dictionary<string, string>
代替Hashtable
吗?
或者
有没有其他方法可以读取键值对来维护它们的配置顺序?
解决方案
推荐阅读
- git - 如何检查git标签中是否存在文件夹
- azure-ad-b2c - 在使用 msal.js 的 Azure B2C 身份验证中,b2cScopes 的含义和语法是什么?
- networking - 有哪些方法可以确保 UDP 按顺序传送数据包?
- angular - 动态渲染组件
- tcp - 为什么 tcp 实现使用字节序列号而不是段号进行流量控制?
- telegram-bot - Webhook Telegram 连续发送多个更新
- javascript - 可以直接用setinterval调用clearinterval吗?
- python - 如何从选择标签硒中选择所有值
- kotlin - 在 Kotlin 中未调用 IsInitialized
- amazon-web-services - SSL:握手超时