简体   繁体   English

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

[英]c# load/save objects or object array

i think this is the code i use , but cannot tell without being able to load it, basically imagebook is a single object which stores "imagedata object array" containing images and integers for my load save. 我认为这是我使用的代码,但无法加载它,但基本上imagebook是一个单独的对象,它存储包含图像和整数的“imagedata对象数组”,用于我的加载保存。 but have problems with "type" error on one of the lines in the load sub. 但是在load子行中的某行上有“type”错误的问题。

if there is a way to save the whole imagedata array and delete imagebook that would be ok too. 如果有办法保存整个imagedata数组并删除也可以的图像书。

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
        }
    }
}

The book1 variable seems to be an ImageBook1 (not sure; you did not share its declaration with us) so you can't assign a dictionary to it. book1变量似乎是一个ImageBook1 (不知道,你不跟我们分享它的声明),所以你不能指定一个字典给它。

Whatever type you serialize should also be the type being deserialized, so you probably want to cast it back to an ImageBook1 : 您序列化的类型也应该是反序列化的类型,因此您可能希望将其ImageBook1转换为ImageBook1

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

If not, you'll have to edit your answer and indicate what type of dictionary you want. 如果没有,您将必须编辑您的答案并指出想要的字典类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM