【发布时间】:2010-12-04 10:43:46
【问题描述】:
我一直在 Properties.Settings.Default 对象中存储用户设置集合,并使用 Visual Studio 设置设计器(右键单击您的项目,单击属性,然后单击设置选项卡)进行设置。最近,一些用户抱怨此特定设置跟踪的数据随机丢失。
给出一个想法(不完全是我怎么做,但有点接近),它的工作方式是我有一个对象,像这样:
class MyObject
{
public static string Property1 { get; set; }
public static string Property2 { get; set; }
public static string Property3 { get; set; }
public static string Property4 { get; set; }
}
然后在代码中,我可能会做这样的事情来保存信息:
public void SaveInfo()
{
ArrayList userSetting = new ArrayList();
foreach (Something s in SomeCollectionHere) // For example, a ListView contains the info
{
MyObject o = new MyObject {
Property1 = s.P1;
Property2 = s.P2;
Property3 = s.P3;
Property4 = s.P4;
};
userSetting.Add(o);
}
Properties.Settings.Default.SettingName = userSetting;
}
现在,提取它的代码是这样的:
public void RestoreInfo()
{
ArrayList setting = Properties.Settings.Default.SettingName;
foreach (object o in setting)
{
MyObject data = (MyObject)o;
// Do something with the data, like load it in a ListView
}
}
我还确保使用 [global::System.Configuration.SettingsSerializeAs(global::System.Configuration.SettingsSerializeAs.Binary)] 装饰 Settings.Designer.cs 文件,如下所示:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SettingsSerializeAs(global::System.Configuration.SettingsSerializeAs.Binary)]
public global::System.Collections.ArrayList SettingName
{
get {
return ((global::System.Collections.ArrayList)(this["SettingName"]));
}
set {
this["SettingName"] = value;
}
}
现在,信息会随机消失。我可以调试它并看到Properties.Settings.Default 正在为SettingName 返回一个空的ArrayList。我真的宁愿不使用 ArrayList,但我没有看到以这种方式存储通用集合的方法。
我即将放弃并自己使用纯 XML 保存这些信息。我只是想验证我确实将这一点 .NET 基础设施推得太远了。我说的对吗?
【问题讨论】: