如何在特定类型的数组存储到我的设置文件?我的、数组、类型、文件

2023-09-03 15:13:23 作者:孤瘾

由于某种原因,我似乎无法给我的类数组存储到设置。这里的code:

For some reason, I can't seem to store an array of my class into the settings. Here's the code:

            var newLink = new Link();
            Properties.Settings.Default.Links = new ArrayList();
            Properties.Settings.Default.Links.Add(newLink);
            Properties.Settings.Default.Save();

在我Settings.Designer.cs我指定的字段是一个数组列表:

In my Settings.Designer.cs I specified the field to be an array list:

    [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类是可序列化,我已经测试过了。

For some reason, it won't save any of the data even though the Link class is serializable and I've tested it.

推荐答案

我找到了问题的根源。只需用普通的数组将不会削减它。考虑这件事后,解串器不知道是什么类型的数组项反序列化到。我没有看到该阵列所需的强类型。设计师导致我愚蠢地认为这是一个普通的通用数组:

I found the source of the problem. Simply using a plain Array won't cut it. After thinking about it, the deserializer wouldn't know what type to deserialize the array items to. I failed to see that the array required strong typing. The designer lead me to foolishly believe it was a plain generic array:

    [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而不是从设计师。

I had to make these changes in the Settings.Designer.cs and not from the designer.