由于某种原因,我似乎无法将我的类的数组存储到设置中。这是代码:
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 中进行这些更改,而不是从设计器中进行。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)