【问题标题】:ApplicationSettingsBase is not serializing my classes as expectedApplicationSettingsBase 没有按预期序列化我的类
【发布时间】:2016-01-30 18:27:35
【问题描述】:

我正在尝试使用 ApplicationSettingsBase 来持久化我的课程。我这里有一个例子。我也尝试过不使用 ApplicationSettingsBase,这似乎工作正常。该示例在下面。

有人能给我一些关于使用 ApplicationSettingsBase 来保持我的完整类层次结构的指导吗?谢谢,谢谢。

我在第一个示例中保存了一些中间实例来尝试解决这个问题。我不打算在最终实施中这样做。我可以在这里看到它会保存中间数据,但不会保存嵌套数据。我只是不明白。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

// prefs is defined in Settings.settings with the name prefs, type WindowsFormsApplication1.Prefs, user scope, and no default value.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Properties.Settings.Default.prefs = new Prefs();

            Test test = new Test();
            test.steps.Add("Step1");
            test.steps.Add("Step2");

            Properties.Settings.Default.prefs.tests.Add(test);

            test.Save();
            Properties.Settings.Default.Save();
            Properties.Settings.Default.prefs.Save();

            Environment.Exit(0);
        }
    }

    // WindowsFormsApplication1.Prefs
    public class Prefs : ApplicationSettingsBase
    {
        [UserScopedSetting]
        [SettingsSerializeAs(SettingsSerializeAs.Xml)]
        public List<Test> tests
        {
            get { return ((List<Test>)(this["tests"])); }
            set { this["tests"] = value; }
        }

        public Prefs()
        {
            tests = new List<Test>();
        }
    }

    public class Test : ApplicationSettingsBase
    {
        [UserScopedSetting]
        [SettingsSerializeAs(SettingsSerializeAs.Xml)]
        public string name
        {
            get { return ((string)(this["name"])); }
            set { this["name"] = value; }
        }

        [UserScopedSetting]
        [SettingsSerializeAs(SettingsSerializeAs.Xml)]
        public List<string> steps
        {
            get { return ((List<string>)(this["steps"])); }
            set { this["steps"] = value; }
        }

        public Test()
        {
            name = "NoName";
            steps = new List<string>();
        }

    }
}

运行后的user.config:

/*
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="WindowsFormsApplication1.Prefs" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <section name="WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        <section name="WindowsFormsApplication1.Test" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<userSettings>
    <WindowsFormsApplication1.Prefs>
        <setting name="tests" serializeAs="Xml">
            <value />
        </setting>
    </WindowsFormsApplication1.Prefs>
    <WindowsFormsApplication1.Properties.Settings>
        <setting name="prefs" serializeAs="Xml">
            <value />
        </setting>
    </WindowsFormsApplication1.Properties.Settings>
    <WindowsFormsApplication1.Test>
        <setting name="steps" serializeAs="Xml">
            <value>
                <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <string>Step1</string>
                    <string>Step2</string>
                </ArrayOfString>
            </value>
        </setting>
        <setting name="name" serializeAs="Xml">
            <value>
                <string>NoName</string>
            </value>
        </setting>
    </WindowsFormsApplication1.Test>
</userSettings>http://stackoverflow.com/questions/13798528/servicestack-text-does-not-serialize-my-object-as-expected
</configuration>
*/

这里是没有尝试使用ApplicationSettingsBase的例子:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

// prefs is defined in Settings.settings with the name prefs, type WindowsFormsApplication1.Prefs, user scope, and no default value.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Properties.Settings.Default.prefs = new Prefs();

            Test test = new Test();
            test.steps.Add("Step1");
            test.steps.Add("Step2");

            Properties.Settings.Default.prefs.tests.Add(test);

            Properties.Settings.Default.Save();

            Environment.Exit(0);
        }
    }

    // WindowsFormsApplication1.Prefs
    public class Prefs
    {
        public List<Test> tests { get; set; }
        public Prefs()
        {
            tests = new List<Test>();
        }
    }

    public class Test
    {
        public string name { get; set; }
        public List<string> steps { get; set; }

        public Test()
        {
            name = "NoName";
            steps = new List<string>();
        }
    }
}

运行后的user.config:

/*
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
</configSections>
<userSettings>
    <WindowsFormsApplication1.Properties.Settings>
        <setting name="prefs" serializeAs="Xml">
            <value>
                <Prefs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    <tests>
                        <Test>
                            <name>NoName</name>
                            <steps>
                                <string>Step1</string>
                                <string>Step2</string>
                            </steps>
                        </Test>
                    </tests>
                </Prefs>
            </value>
        </setting>
    </WindowsFormsApplication1.Properties.Settings>
</userSettings>
</configuration>
*/

【问题讨论】:

    标签: c# serialization applicationsettingsbase


    【解决方案1】:

    This answer 几乎解决了我的问题:

    特别是,Properties.Settings.Default 通常会返回一个 名为 Settings 的 Designer 创建的类的实例。打电话 Properties.Settings.Default.Save(); 将只保存其中的值 对象,而不是其他类中的对象。

    如果您有一个单独的课程DeviceConfiguration 想要保存 (正如您在发布的代码中所看到的那样),您需要处理 明确地。只需拥有一个子类的实例 ApplicationSettingsBase 不会这样做。您需要拨打Save() 自定义子类上的方法。

    我没有意识到我的自定义结构不必放置在 Properties.Settings 中就可以保存到 user.config 文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多