出于某种原因,我似乎无法将我的类的数组存储到设置中。代码如下:
var newLink = new Link();
Properties.Settings.Default.Links = new ArrayList();
Properties.Settings.Default.Links.Add(newLink);
Properties.Settings.Default.Save();
在我的 Settings.Designer.cs 中,我指定该字段为数组列表:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.ArrayList Links {
get {
return ((global::System.Collections.ArrayList)(this["Links"]));
}
set {
this["Links"] = value;
}
}
出于某种原因,即使 Link 类是可序列化的并且我已经对其进行了测试,它也不会保存任何数据。
最佳答案
我找到了问题的根源。仅仅使用普通数组是不够的。经过思考,反序列化器不知道将数组项反序列化为什么类型。我没有看到数组需要强类型。设计者让我愚蠢地相信这是一个普通的通用数组:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public List<Link> Links
{
get {
return ((List<Link>)(this["Links"]));
}
set {
this["Links"] = value;
}
}
我必须在 Settings.Designer.cs 中进行这些更改,而不是从设计器中进行。
关于.net - 如何将特定类型的数组存储到我的设置文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2761924/