gpt4 book ai didi

c# 加载/保存对象或对象数组

转载 作者:行者123 更新时间:2023-11-30 17:40:50 24 4
gpt4 key购买 nike

我认为这是我使用的代码,但如果不能加载它就无法判断,基本上 imagebook 是一个单一的对象,它存储包含图像和整数的“图像数据对象数组”,供我加载保存。但在加载子中的其中一行出现“类型”错误的问题。

如果有一种方法可以保存整个 imagedata 数组并删除 imagebook 那也可以。

namespace FastCrack
{
[Serializable]
class ImageBook1
{
public ImageData1[] imgdat;
public ImageBook1(ImageData1[] dat) {
imgdat = new ImageData1[1000];
imgdat = dat;
}
}
}




namespace FastCrack
{
public partial class Form1 : Form
{
private void saveCrack()
{
try
{
book1 = new ImageBook1(imagedataSheets);
// Create a FileStream that will write data to file.
FileStream writerFileStream = new FileStream(Filename1, FileMode.Create, FileAccess.Write); // Save our dictionary of friends to file
this.formatter.Serialize(writerFileStream, this.book1);
writerFileStream.Close();
}
catch (Exception) {
Console.WriteLine("Unable to save our friends' information");
} // end try-catch
}


private void loadCrack()
{

if (File.Exists(Filename1))
{

try
{
// Create a FileStream will gain read access to the
// data file.
FileStream readerFileStream = new FileStream(Filename1,
FileMode.Open, FileAccess.Read);
// Reconstruct information of our friends from file.




//this.friendsDictionary = (Dictionary<String, Friend>
//<--this is line from example

this.book1 = (Dictionary<ImageBook1, book1>)
// (Dictionary<String, Friend>)
<-- this is the line giving me type error




this.formatter.Deserialize(readerFileStream);
// Close the readerFileStream when we are done
readerFileStream.Close();

}
catch (Exception)
{
Console.WriteLine("There seems to be a file that contains " +
"friends information but somehow there is a problem " +
"with reading it.");
} // end try-catch

} // end if
}
}
}

最佳答案

book1 变量似乎是一个 ImageBook1(不确定;您没有与我们分享它的声明)所以您不能为其分配字典。

无论您序列化什么类型也应该是被反序列化的类型,因此您可能想要将它转换回 ImageBook1:

this.book1 = (ImageBook1)this.formatter.Deserialize(readerFileStream);

如果没有,您将不得不编辑您的答案并指明想要的字典类型。

关于c# 加载/保存对象或对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com