【问题标题】:Retrieving Dictionary<string, int> from app.config using DictionarySectionHandler使用 DictionarySectionHandler 从 app.config 中检索 Dictionary<string, int>
【发布时间】:2016-07-13 04:32:50
【问题描述】:

我正在尝试在 app.config 中存储字典。在使用 NameValueCollectionSectionHandler 时避免解析每个字符串会非常有用,所以我转而使用 DictionarySectionHandler。

我已经能够导入一个哈希表,但我似乎只能获取字符串值。有没有办法直接访问字典而不解析所有内容?

有没有直接导入字典的方法?我似乎无法让 DictionarySectionHandler 给我一个实际的字典。

【问题讨论】:

    标签: c# .net app-config


    【解决方案1】:

    DictionarySectionHandler 将始终返回一个 HashTable

    您只需使用您想要的类型将其转换为字典

    例如,如果您的 Dictionary 值是整数,您可以这样做

    Dictionary<string,int> targetDictionary = sourceHashtable.Cast<DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (int)d.Value);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多