【发布时间】:2014-08-19 19:06:21
【问题描述】:
我有一个类,我希望在我的设置文件中保存它的实例。我无法使用常规 properties.settings.default.save() 命令保存类。不会出现错误,但设置不会在运行之间持续存在,我理解这是序列化的问题。
我已将问题定位到一个特定的行,即公共多维 int 数组。如果我将二维数组更改为一维数组,它可以正常工作,但二维数组更适合我的应用程序。由于我仍在学习,我想具体了解为什么会发生这种情况。
我的问题是,为什么二维数组不能保存到设置中?是序列化的问题吗?我怎样才能正确地实现它?
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.
}
【问题讨论】:
-
既然你正在学习这个网站将非常适合你如何设置你需要的东西dontetperls 2D array
-
谢谢。 Dotnetperls 已经为我提供了巨大的帮助!
标签: c# class serialization save settings