如何使用设置在自定义类中保存多维数组

时间:2014-08-19 19:06:21

标签: c# class serialization save settings

我有一个类,我希望在我的设置文件中保存实例。我无法使用常规properties.settings.default.save()命令保存该类。不会出现错误,但设置不会在运行之间保持不变,我理解这是序列化的问题。

我已经将问题指向了一个特定的行,一个公共的多维int数组。如果我将2d数组更改为1d数组,它可以正常工作,但我的应用程序首选2d数组。在我还在学习的时候,我想知道为什么会这样。

我的问题是,为什么二维数组不会保存到设置中?这是序列化的问题吗?我该如何正确实现?

public class AsmbLine 
{

    public int linenumber { get; set; }
    public Ticket[] ticketlist { get; set; }
    public string[] platesizes { get; set; }
    public string[] platetypes { get; set; }
    public string[] platecounts { get; set; }
    public int[,] cellidsallowed { get; set; } // This Line is the problem

    //public int[] cellidsallowed { get; set; } //uncomment these two lines to fix
    //public int[] cellidsallowedtypes { get; set; } // uncomment these two lines to fix

干杯

**编辑**

以下是我访问/保存设置的方法。我发现在使用自定义类时最容易使用单独的bool来标记设置是否在第一次启动时初始化

private void asmbinitializedcheckandload()
    {
            bool temp = false;
            temp = Properties.Settings.Default.Asmbarrayinitialized;
            if (temp == false) // if this is false, then we are running for the first time
            {
                Optimo20.Properties.Settings.Default.alines = new AsmbLine[maxlines]; //create new instance of alines[]

                Properties.Settings.Default.Save(); // save

                for (int i = 0; i < Properties.Settings.Default.alines.Length; i++)
                {
                    Optimo20.Properties.Settings.Default.alines[i] = new AsmbLine(); // initialize each member of alines


                }    
                Properties.Settings.Default.Asmbarrayinitialized = true; // set the flag to true, setting is initialized
                Properties.Settings.Default.Save(); // save
             }

        alinearray = Optimo20.Properties.Settings.Default.alines; // load settings data into local array.


    }

1 个答案:

答案 0 :(得分:0)

将代码更改为此后会发生什么?这应该编译

public class AsmbLine 
{
    public int linenumber { get; set; }
    public Ticket[] ticketlist { get; set; }
    public string[] platesizes { get; set; }
    public string[] platetypes { get; set; }
    public string[] platecounts { get; set; }
    //public int[] cellidsallowed { get; set; } //uncomment these two lines to fix
    private int[,] cellidsallowed;  

    public int[,] Cellidsallowed         
    {   get { return cellidsallowed; } 
        set { cellidsallowed = value; }         
    }
}