[英]ApplicationSettingsBase is not serializing my classes as expected
我正在嘗試使用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>
*/
這個答案幾乎釘住了我的問題:
特別是,
Properties.Settings.Default
通常將返回由Designer創建的名為Settings
類的實例。 調用Properties.Settings.Default.Save();
將僅保存該對象中的值,而不保存其他類中的值。如果您有要保存的單獨的類
DeviceConfiguration
(如發布的代碼中所示),則需要顯式處理。 僅擁有ApplicationSettingsBase
子類的實例將無法實現。 您需要自己在該自定義子類上調用Save()
方法。
我沒有意識到不必將我的自定義結構放置在Properties.Settings
中才能將其保存到user.config
文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.