使用下面的示例,有人可以向我解释为什么List<Foo>
在显式设置 Foo 的每个属性的地方添加一个新的会导致该ApplicationSettingsBase.Save()
方法正确存储数据,而通过构造函数将新的 Foo 添加到列表中(构造函数设置属性值) 不工作?谢谢!
public class Foo
{
public Foo(string blah, string doh)
{
this.Blah = blah;
this.Doh = doh;
}
public Foo() { }
public string Blah { get; set; }
public string Doh { get; set; }
}
public sealed class MySettings : ApplicationSettingsBase
{
[UserScopedSetting]
public List<Foo> MyFoos
{
get { return (List<Foo>)this["MyFoos"]; }
set { this["MyFoos"] = value; }
}
}
// Here's the question...
private void button1_Click(object sender, EventArgs e)
{
MySettings mySettings = new MySettings();
// Adding new Foo's to the list using this block of code doesn't work.
List<Foo> theList = new List<Foo>()
{
new Foo("doesn't","work")
};
// But using this block of code DOES work.
List<Foo> theList = new List<Foo>()
{
new Foo() {Blah = "DOES", Doh = "work"}
};
// NOTE: I never ran both the above code blocks simultaneously. I commented
// one or the other out each time I ran the code so that `theList` was
// only created once.
mySettings.MyFoos = theList;
mySettings.Save();
}