如何在特定类型的数组存储到我的设置文件? [英] How do I store an array of a particular type into my settings file?

查看:211
本文介绍了如何在特定类型的数组存储到我的设置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我似乎无法给我的类数组存储到设置。这里的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.

这篇关于如何在特定类型的数组存储到我的设置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆