首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ApplicationSettingsBase没有按预期序列化我的类

ApplicationSettingsBase没有按预期序列化我的类
EN

Stack Overflow用户
提问于 2015-12-23 19:58:41
回答 1查看 819关注 0票数 1

我试图使用ApplicationSettingsBase来持久化我的类。我这里有个例子。我也尝试过不使用ApplicationSettingsBase,这似乎很好。下面是这个例子。

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

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

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
/*
<?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>
*/

下面是不尝试useApplicationSettingsBase的示例:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
/*
<?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>
*/
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-29 18:23:36

This answer很好地回答了我的问题:

特别是,Properties.Settings.Default通常会返回一个名为Settings的设计器创建的类的实例。调用Properties.Settings.Default.Save();只会保存该对象中的值,而不会保存其他类中的值。 如果您有一个单独的类DeviceConfiguration,您需要保存它(就像在您发布的代码中显示的那样),您需要显式地处理它。仅仅拥有一个ApplicationSettingsBase子类的实例是做不到的。您需要自己调用自定义子类上的Save()方法。

我没有意识到我的自定义结构不需要放在Properties.Settings中才能保存到user.config文件中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34442843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档