C#Serialization Unknow问题

时间:2017-02-02 07:52:01

标签: c# serialization

您好我尝试seriliaze文件,但我有一个非常 奇怪的问题。

首次保存时,所有变量都已正确序列化。 接下来,许多变量仍为空,其他变量保存。

1°序列化代码

   public void SaveMireProject(string projectName,string root_path)
        {
            BinaryFormatter bin = new BinaryFormatter();

            using (FileStream _FileStream =
                    new FileStream(root_path + "\\" + projectName + ".mireprj", System.IO.FileMode.Create, FileAccess.Write))
            {
                //bin.Serialize(_FileStream, this);

                bin.Serialize(_FileStream, Tiles);
                bin.Serialize(_FileStream, project_name);
                bin.Serialize(_FileStream, root_directory);
                try { bin.Serialize(_FileStream, project_pat); } catch { }
                try { bin.Serialize(_FileStream, project_type); } catch { }
                bin.Serialize(_FileStream, Events);
                bin.Serialize(_FileStream, Light);
                bin.Serialize(_FileStream, StartupMap);
                bin.Serialize(_FileStream, startX);
                bin.Serialize(_FileStream, startY);
                _FileStream.Close();
                _FileStream.Dispose();
            }
        }

2°去污代码

 public void OpenProject(string filename)
        {
            BinaryFormatter bin = new BinaryFormatter();
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {

                Tiles = (List<_Tiles>)bin.Deserialize(fs);
                project_name = (string)bin.Deserialize(fs);
                root_directory = (string)bin.Deserialize(fs);

                try { Events = (List<_ev>)bin.Deserialize(fs); } catch { Events = new List<_ev>(); };

                try { Light = (List<_light>)bin.Deserialize(fs); } catch { Light = new List<_light>(); };

                try { project_pat = (string)bin.Deserialize(fs); } catch { };
                try { project_type = (string)bin.Deserialize(fs); } catch { };

                try { StartupMap = (string)bin.Deserialize(fs); } catch { };
                try { startX = (int)bin.Deserialize(fs); } catch { }
                try { startY = (int)bin.Deserialize(fs); } catch { }

            }

        }

一些重要的变量是

StartupMap,startX和startY。

设定值变量后, 我看到whit调试器,所有值都存储起来。

enter image description here

我设置断点以保存线路。 我看到价值在变化中

接下来,我尝试打开现在文件序列化, 我看到了这个

enter image description here

调用OpenProject方法后设置的断点。

谁知道有什么问题?

1 个答案:

答案 0 :(得分:2)

你序列化

 try { bin.Serialize(_FileStream, project_pat); } catch { }
 try { bin.Serialize(_FileStream, project_type); } catch { }
 bin.Serialize(_FileStream, Events);
 bin.Serialize(_FileStream, Light);

你反序列化如下:

try { Events = (List<_ev>)bin.Deserialize(fs); } catch { Events = new List<_ev>(); };
try { Light = (List<_light>)bin.Deserialize(fs); } catch { Light = new List<_light>(); };
try { project_pat = (string)bin.Deserialize(fs); } catch { };
try { project_type = (string)bin.Deserialize(fs); } catch { };

序列化和反序列化的顺序不正确你也不能把try catch,因为如果某些失败序列化和反序列化每个项目,将改变订单......

尝试serlize,如:

bin.Serialize(_FileStream, Tiles);
bin.Serialize(_FileStream, project_name);
bin.Serialize(_FileStream, root_directory);
bin.Serialize(_FileStream, project_pat); 
bin.Serialize(_FileStream, project_type);
bin.Serialize(_FileStream, Events);
bin.Serialize(_FileStream, Light);
bin.Serialize(_FileStream, StartupMap);
bin.Serialize(_FileStream, startX);
bin.Serialize(_FileStream, startY);

并反序列化如:

Tiles = (List<_Tiles>)bin.Deserialize(fs);
project_name = (string)bin.Deserialize(fs);
root_directory = (string)bin.Deserialize(fs);
project_pat = (string)bin.Deserialize(fs);
project_type = (string)bin.Deserialize(fs); 
Events = (List<_ev>)bin.Deserialize(fs);
Light = (List<_light>)bin.Deserialize(fs);
StartupMap = (string)bin.Deserialize(fs);
startX = (int)bin.Deserialize(fs);
startY = (int)bin.Deserialize(fs);

订单正确