gpt4 book ai didi

c# - 这是对设置功能的滥用吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:09 25 4
gpt4 key购买 nike

我一直在 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 基础结构推得太远了。我说得对吗?

最佳答案

我找不到关于为什么这些设置会消失的答案,而且由于我不断发生这种情况,我最终将复杂的设置集单独存储在一个 XML 文件中,自己手动序列化和反序列化它们。

关于c# - 这是对设置功能的滥用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1528435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com