c# - 将哈希表添加到设置

标签 c# winforms settings hashtable app-config

我遇到了一个让我发疯的问题。我想将哈希表添加到设置中(并添加了 2 个哈希表),但是当我尝试使用它时,它抛出了一个异常(对象引用未设置为对象的实例。)。然后我查看了 app.config,它看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
   <configuration>
     <configSections>
     </configSections>

   </configuration>

然后我在 app.config 文件中添加了两个哈希表,现在我的配置文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>

    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="APPNAME.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>

<userSettings>
    <APPNAME.Properties.Settings>
      <setting name="UserInfo" serializeAs="System.Collections.Hashtable">
        <value />
      </setting>
      <setting name="UserText" serializeAs="System.Collections.Hashtable">
        <value />
      </setting>
    </APPNAME.Properties.Settings>
</userSettings>

当我尝试使用哈希表时,它抛出了以下异常: 无法解析属性“serializeAs”的值。

我用谷歌搜索过但没有成功,我在msdn上搜索也没有找到。

最佳答案

哈希表不支持序列化为 XML。您在 Settings.Settings 中只有 2 个可用选项。 XML 或字符串。将您的序列化选项更改为

serializeAs="Binary"

与您的设置类类似,您必须将序列化选项属性指定为

[SettingsSerializeAs(SettingsSerializeAs.Binary)]

关于c# - 将哈希表添加到设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17900408/

相关文章:

c# - 交易失败。服务器响应是 : SMTP Host

c# - 在 WinForms WebBrowser 中编辑 CSS

.net - 保存和恢复表单位置和大小

c# - 在 C# 中处理 ListBox 项上的双击事件

c# - 如何反序列化接口(interface)类型?

c# - 如何根据进程 ID 而不是进程名称获取 CPU 使用率

javascript - 仅在加载网格时显示进度

css - Visual Studio 中颜色名称下的颜色线

cursor - Sublime Text 2 光标突出显示下一个字符...为什么?

ios - 我们可以访问用户在 iOS 中设置的自动锁定设置吗?