היי, ניסתי לעשות סרנלזציה לאוסף גנרי Dictionary.
משום מה שאני מנסה לעשות דיסרנלזציה זה זורק לי אקספשן כזה:
------------------------------------------------------ System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed. at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at SVR.myDictionary.Load(String path) in c:\users\aviad\documents\visual studio 2010\Projects\SVR\SVR\myDictionary.cs:line 52 --------------------------- OK ---------------------------
|
וככה ניסתי לעשות את זה:
public delegate void FirstTimeLoadingHandler(); public event FirstTimeLoadingHandler OnFirstTimeLoadingEvent; Dictionary<string,Process> dictionary; BinaryFormatter bf; public myDictionary() { this.dictionary = new Dictionary<string, Process>(); bf = new BinaryFormatter(); } public void Save(string path) { using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) { bf.Serialize(fs, dictionary); } } /// <summary> /// Load, loads the dictionary collection from a certain path as a binary file. /// if there is nothing to load you are advice to load manually by using /// the "OnFirstTimeLoadingEvent" , after the event is executed this method saves the dictionary. /// </summary> /// <param name="path"></param> public void Load(string path) { FileStream fs = null; try { fs = File.OpenRead(path); dictionary = (Dictionary<string, Process>)bf.Deserialize(fs); } catch (FileNotFoundException) { //means that this is the first time in the software, so we save if (OnFirstTimeLoadingEvent != null) OnFirstTimeLoadingEvent(); if (dictionary.Count > 0) Save(path); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } finally { if (fs != null) fs.Dispose(); } }
|
אשמח לקבל עזרה בהקדם...