如何将自定义类保存/序列化到设置文件?

问题描述 投票:0回答:5

我有一个小类,包含两个字符串,如下所示:

    public class ReportType
    {
        private string displayName;
        public string DisplayName
        {
            get { return displayName; }
        }

        private string reportName;
        public string ReportName
        {
            get { return reportName; }
        }

        public ReportType(string displayName, string reportName)
        {
            this.displayName = displayName;
            this.reportName = reportName;
        }
    }

我想将此类的实例保存到我的设置文件中,以便我可以执行以下操作:

ReportType reportType = Settings.Default.SelectedReportType;

谷歌搜索似乎表明这是可能的,但似乎没有任何明确的指南可供我遵循。我知道需要进行一些序列化,但真的不知道从哪里开始。另外,当我进入 Visual Studio 中的“设置”屏幕并单击“类型”列下的“浏览”时,没有选项可以选择包含 ReportType 类的当前命名空间。

c# visual-studio settings
5个回答
19
投票

好吧,我想我最终已经解决了。首先要做的就是为ReportType类需要序列化的每个属性添加以下属性,并继承自ApplicationSettingsBase的类:

    public class ReportType : ApplicationSettingsBase
    {
        private string displayName;
        [UserScopedSetting()]
        [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
        public string DisplayName
        {
            get { return displayName; }
        }

..............

然后,一旦重建了程序集(重要!),您可以进入设置屏幕并单击“浏览”,然后在底部的文本框中键入您的命名空间和类名称(例如 Label_Creator.ReportType)。命名空间和类名不会出现在树中,因此这部分并不完全明显您需要做什么,这就是为什么它有点令人困惑......


4
投票

@Calanus 解决方案对我来说不起作用(在 Visual Studio 2015 上)。 缺少的步骤是实际设置或从实际设置获取。 至于原来的问题,实现一个简单的 POCO 可以这样实现:

[Serializable]
public class ReportType
{
    public string DisplayName { get; set; }
    public string ReportName { get; set; }

    public ReportType() { }

    public ReportType(string displayName, string reportName)
    {
        DisplayName = displayName;
        ReportName = reportName;
    }
}

// the class responsible for reading and writing the settings
public sealed class ReportTypeSettings : ApplicationSettingsBase
{
    [UserScopedSetting]
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    [DefaultSettingValue("")]
    public ReportType ReportType
    {
        get { return (ReportType)this[nameof(ReportType)]; }
        set { this[nameof(ReportType)] = value; }
    }
}

我实际上已经使用了一个列表来序列化:

[Serializable]
public class Schedule
{
    public Schedule() : this(string.Empty, DateTime.MaxValue)
    {
    }

    public Schedule(string path, DateTime terminationTime)
    {
        path = driverPath;
        TerminationTime = terminationTime;
    }

    public DateTime TerminationTime { get; set; }
    public string Path { get; set; }
}

public sealed class Schedules : ApplicationSettingsBase
{
    [UserScopedSetting]
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    [DefaultSettingValue("")]
    public List<Schedule> Entries
    {
        get { return (List<Schedule>)this[nameof(Entries)]; }
        set { this[nameof(Entries)] = value; }
    }
}

实例化 Schedules (ReportTypeSettings) 对象。它会自动读取设置。可以使用Reload方法来刷新。 例如:

ReportTypeSettings rts = new ReportTypeSettings();
rts.Reload();
rts.ReportType = new ReportType("report!", "report1");
rts.Save();

重要注意事项

  1. 请注意,UserScoped 是有意使用的。 ApplicationScoped 的行为有所不同,因此请确保您知道自己在做什么。
  2. 成员是公共的(包括设置器),并且有一个默认构造函数,即使代码需要它。但是,使用 XML 进行序列化无法正常工作。由于时间有限,我没有调查。 您也可以将序列化更改为二进制格式。它将使用 BASE64 来存储数据。
  3. 所有设置都存储在 LOCAL APP DATA 文件夹中的文本文件中。

3
投票
Charlie 的

更清晰一点的代码 public class ReportType { public static ReportType CreateDefaults() { return new ReportType { DisplayName = ConfigurationManager.AppSettings["DefaultDisplayName"], ReportName = ConfigurationManager.AppSettings["DefaultReportName"] }; } }



2
投票

public class ReportType { public static ReportType GetDefaultSelectedReportType() { string displayName = ConfigurationManager.AppSettings["DefaultDisplayName"]; string reportName = ConfigurationManager.AppSettings["DefaultReportName"]; return new ReportType(displayName, reportName); } . . . }



0
投票

创建类型/类 按照原始海报创建一个类,但要注意两个重要细节:

该类必须是

public

,并且包含一个不带任何参数的构造函数,以充当该类的默认状态。我找不到这方面的任何官方文档,但根据我的经验,这是通过“选择类型”对话框成功序列化和识别所必需的。

该类必须包含在您定义应用程序设置的当前程序集的(如果可能)或纯 XML。请注意,您还可以使用

TypeConverter

来控制序列化的执行方式。

© www.soinside.com 2019 - 2024. All rights reserved.