如何在应用程序设置中存储对象列表 [英] How to store a list of objects in application settings

查看:176
本文介绍了如何在应用程序设置中存储对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近熟悉了C#应用程序设置,这看起来很酷。

我正在寻找一种存储自定义对象列表的方法,但是我找不到方法! >
实际上,我看到了发布存储int [] ,但是对这个问题没有帮助。

我试图更改该解决方案的配置,以使其适合于我的问题。的XML配置文件为:

I have recently became familiar with C# application settings, and it seems cool.
I was searching for a way to store a list of custom objects, but I couldn't find a way!
Actually I saw a post to store int[], but it wasn't helpful for this problem.
I tried to change the config of that solution in order to make it suitable for my problem. the XML config file of that was:

<Setting Name="SomeTestSetting" Type="System.Int32[]" Scope="User">
  <Value Profile="(Default)" />
</Setting>

我试图按下面在type属性中引用的方式处理我的对象,但这没有帮助无法识别我的对象...我尝试了 type = List和 type = tuple []

这两个选项都对我没有帮助!

I tried to address my object as quoted below in the type attribute but it wasn't helpful since it doesn't recognizing my object... I tried "type = List" and "type="tuple[]"
both of these options didn't help me!

我有一个班级看起来像:

I have a class looks like:

class tuple
    {
        public tuple()
        {
            this.font = new Font ("Microsoft Sans Serif",8);
            this.backgroundcolor_color = Color.White;
            this.foregroundcolor_color = Color.Black;
        }
        public string log { get; set; }
        public Font font { get ; set; }
        public String fontName { get; set; }
        public string foregroundcolor { get; set; }
        public Color foregroundcolor_color { get; set; }
        public string backgroundcolor { get; set; }
        public Color backgroundcolor_color { get; set; }
        public Boolean notification { get; set; }
    }

,我想在应用程序设置中存储一个列表。

那么有什么方法可以达到这个目的。

提前谢谢。

喝彩,

and I want to store a list in application setting.
So is there any way to achieve this purpose.
Thanks in advance.
Cheers,

推荐答案

您可以使用 BinaryFormatter 将元组列表序列化为字节数组,而Base64(以非常有效的方式)将字节数组存储为 string

You can use BinaryFormatter to serialize list of tuples as byte array and Base64 (as quite efficient way) to store byte array as string.

首先将类更改为类似的内容(提示: [SerializableAttribute] ):

First of all change your class to something like that (hint: [SerializableAttribute]):

[Serializable()]
public class tuple
{
    public tuple()
    {
        this.font = new Font("Microsoft Sans Serif", 8);
    //....
}

在名为元组和类型 string

然后,您可以使用两种方法来加载和保存元组的通用列表( List< tuple> ):

Then you can use two methods to load and save generic list of tuples (List<tuple>):

void SaveTuples(List<tuple> tuples)
{
    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(ms, tuples);
        ms.Position = 0;
        byte[] buffer = new byte[(int)ms.Length];
        ms.Read(buffer, 0, buffer.Length);
        Properties.Settings.Default.tuples = Convert.ToBase64String(buffer);
        Properties.Settings.Default.Save();
    }
}

List<tuple> LoadTuples()
{
    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.tuples)))
    {
        BinaryFormatter bf = new BinaryFormatter();
        return (List<tuple>)bf.Deserialize(ms);
    }
}

示例:

List<tuple> list = new List<tuple>();
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());

// save list
SaveTuples(list);

// load list
list = LoadTuples();

我留下 null ,空字符串和异常检查你。

I leave null, empty string and exception checking up to you.

这篇关于如何在应用程序设置中存储对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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