.net - 如何将特定类型的数组存储到我的设置文件中?

标签 .net settings settings.settings

出于某种原因,我似乎无法将我的类的数组存储到设置中。代码如下:

            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/

相关文章:

c# - 使用设置时如何判断 exe.config 的值是否无效

.net - ASP + 限制子页面使用 css 文件

c# - LINQ:分组子组

c# - 检查表单是否加载

c# - 如何从 .NET 以编程方式调用 SSIS?

PostgreSQL : Can I retrieve log_min_duration_statement as an integer?

android - 如何在Android中获取此音量设置?

C#:如何在尝试从另一个程序集中使用设置变量之前确保其存在?

c# - 如何确定正在使用哪个 C# 设置文件

php - Apigility 中的水化器设置被忽略