本文介绍了存储泛型List< CustomObject>使用ApplicationSettingsBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存一个名单,其中,富>使用ApplicationSettingsBase,但是它只输出以下,即使列表是填充:
I am trying to save a List<Foo> using ApplicationSettingsBase, however it only outputs the following even though the list is populated:
<setting name="Foobar" serializeAs="Xml">
<value />
</setting>
富定义如下:
Foo is defined as follows:
[Serializable()]
public class Foo
{
public String Name;
public Keys Key1;
public Keys Key2;
public String MashupString
{
get
{
return Key1 + " " + Key2;
}
}
public override string ToString()
{
return Name;
}
}
我怎样才能使ApplicationSettingsBase存储列表&LT;富&GT;
How can I enable ApplicationSettingsBase to store List<Foo>?
推荐答案
同意与托马斯Levesque的:
Agreed with Thomas Levesque:
下面的类被正确保存/回读:
The following class was correctly saved/read back:
public class Foo
{
public string Name { get; set; }
public string MashupString { get; set; }
public override string ToString()
{
return Name;
}
}
请注意:我并不需要 SerializableAttribute
编辑:这里的XML输出:
here is the xml output:
<WindowsFormsApplication1.MySettings>
<setting name="Foos" serializeAs="Xml">
<value>
<ArrayOfFoo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Foo>
<Name>Hello</Name>
<MashupString>World</MashupString>
</Foo>
<Foo>
<Name>Bonjour</Name>
<MashupString>Monde</MashupString>
</Foo>
</ArrayOfFoo>
</value>
</setting>
</WindowsFormsApplication1.MySettings>
和设置类我用:
sealed class MySettings : ApplicationSettingsBase
{
[UserScopedSetting]
public List<Foo> Foos
{
get { return (List<Foo>)this["Foos"]; }
set { this["Foos"] = value; }
}
}
,最后我插入的项目:
And at last the items I inserted:
private MySettings fooSettings = new MySettings();
var list = new List<Foo>()
{
new Foo() { Name = "Hello", MashupString = "World" },
new Foo() { Name = "Bonjour", MashupString = "Monde" }
};
fooSettings.Foos = list;
fooSettings.Save();
fooSettings.Reload();
这篇关于存储泛型List&LT; CustomObject&GT;使用ApplicationSettingsBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!