反序列化无法列出数据

时间:2015-10-27 21:16:22

标签: c# visual-studio serialization

我正在尝试创建一个可以序列化和反序列化数据的应用程序,但我可以序列化信息,但是当我尝试读取我留下的空列表信息时,我不知道为什么。

我的序列化类

  [Serializable()]
  public class FileSerilizeObject 
    {
        public static string FileName { get; set; }
        public static string Extension { get; set; }
        public static string Base64 { get; set; } 

        public FileSerilizeObject(string filename, string extension, string base64vaulue)
        {
            FileName = filename;
            Extension = extension;
            Base64 = base64vaulue;
        }

    }
}

我的序列化/反序列化方法

 public void Serialize(List<FileSerilizeObject> List)
        {
            using (Stream stream = File.Open(savepath, FileMode.OpenOrCreate))
            {
                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(stream, List);
                stream.Close();
            }

        }

        public List<FileSerilizeObject> Deserialised(string OpenPath)
        {
            List<FileSerilizeObject> defo;
            using(Stream stream = File.Open(OpenPath, FileMode.Open))
            {

                BinaryFormatter bin = new BinaryFormatter();
                defo = (List<FileSerilizeObject>)bin.Deserialize(stream);
            }

            return defo;
        }

我已经检查过以确保文件路径是正确的并且文件本身不是空的。一切都很好但是&#34; defo&#34; list总是空的,所以我只能假设问题出在defo = (List<FileSerilizeObject>)bin.Deserialize(stream);行,但我不知道为什么。

3 个答案:

答案 0 :(得分:0)

尝试为每个属性设置[Serializable()]属性。

所以它看起来像这样:

[Serializable()]
public class FileSerilizeObject 
    {
        [Serializable()]
        public string FileName { get; set; }
        [Serializable()]
        public string Extension { get; set; }
        [Serializable()]
        public string Base64 { get; set; } 

        public FileSerilizeObject(string filename, string extension, string base64vaulue)
        {
            FileName = filename;
            Extension = extension;
            Base64 = base64vaulue;
        }

    }
}

编辑:从属性中删除了static个关键字。

答案 1 :(得分:0)

您需要从属性中删除静态

[Serializable]
public class FileSerializeObject 
{
    public string FileName { get; set; }
    public string Extension { get; set; }
    public string Base64 { get; set; } 

    public FileSerializeObject(string filename, string extension, string base64vaulue)
    {
        FileName = filename;
        Extension = extension;
        Base64 = base64vaulue;
    }

}

答案 2 :(得分:0)

我在一个控制台应用程序中测试了你的代码并且它适用于我,我使用VS 2013进行了测试,但我使用了你在上面编写的相同代码。

一些细节: 1.我删除了静态表格&#34; FileSerilizeObject&#34;

类FileSerilizeObject

 [Serializable()]
public class FileSerilizeObject
{
    public string FileName { get; set; }
    public string Extension { get; set; }
    public string Base64 { get; set; }

    public FileSerilizeObject(string filename, string extension, string base64vaulue)
    {
        FileName = filename;
        Extension = extension;
        Base64 = base64vaulue;
    }

}

<强>功能

public static void Serialize(List<FileSerilizeObject> List)
    {
        using (Stream stream = File.Open(@"C:\Users\ttest\Desktop\folder1\data.bin", FileMode.OpenOrCreate))
        {
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, List);
            stream.Close();
        }

    }

    public static List<FileSerilizeObject> Deserialised(string OpenPath)
    {
        List<FileSerilizeObject> defo;
        using (Stream stream = File.Open(OpenPath, FileMode.Open))
        {

            BinaryFormatter bin = new BinaryFormatter();
            defo = (List<FileSerilizeObject>)bin.Deserialize(stream);
        }

        return defo;
    }

主要

var bytes = Encoding.UTF8.GetBytes("dffesdbcdef==");
        var base64 = Convert.ToBase64String(bytes);
        FileSerilizeObject f1 = new FileSerilizeObject("test", "jpg", base64);
        bytes = Encoding.UTF8.GetBytes("ggasddbcdef==");
        base64 = Convert.ToBase64String(bytes);
        FileSerilizeObject f2 = new FileSerilizeObject("test2", "png", base64);
        bytes = Encoding.UTF8.GetBytes("asddffasdasdasdesdbcdef==");
        base64 = Convert.ToBase64String(bytes);
        FileSerilizeObject f3 = new FileSerilizeObject("test3", "doc", base64);


        List<FileSerilizeObject> lFiles = new List<FileSerilizeObject>();
        lFiles.Add(f1);
        lFiles.Add(f2);
        lFiles.Add(f3);


        Serialize(lFiles);
        Deserialised(@"C:\Users\rjimen4x\Desktop\tutoriales\data.bin");