2012-06-11 65 views
-3

我在線上關注this tutorial,但不知何故,它給了我錯誤。說沒有對象映射或什麼的。C#,無法序列化爲二進制

我有以下的,我想序列化靜態對象:

[Serializable] 
public class Settings : ISerializable 
{ 
    public static string server= "http://localhost/"; 
    public static string username = "myname"; 
    public static bool savePassword = true; 
    public static bool autoSync = true; 
    public static string password = "mypass"; 
    public static string folderPath1= "c:/"; 
    public static string folderPath2= "c:/"; 
    public static string autoSyncDuration = "300"; 
    public static string lastSyncTime = "???"; 


    public Settings() 
    { } 

    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     Type myTypeObj = Type.GetType("Settings"); 
     foreach (FieldInfo p in myTypeObj.GetFields()) 
     { 
      Object value = p.GetValue(null); 
      info.AddValue(p.Name, value, p.GetType()); 
     } 
    } 

    public Settings(SerializationInfo info, StreamingContext context) 
    { 
     Type myTypeObj = Type.GetType("Settings"); 
     FieldInfo p; 
     foreach (SerializationEntry e in info) 
     { 
      p = myTypeObj.GetField(e.Name); 
      p.SetValue(null, e.Value); 
     } 
    } 
} 

,這裏是讀/寫功能:

private void writeSettings() 
    { 
     pcb_savingSettings.Visible = true; 
     FileStream fileStream = new FileStream(settingFile, FileMode.Create, FileAccess.Write, FileShare.None); 
     BinaryFormatter bf = new BinaryFormatter(); 
     bf.Serialize(fileStream, new Settings()); 

     fileStream.Close(); 
     pcb_savingSettings.Visible = false; 
    } 
    private void readSettings() 
    { 
     if (!File.Exists(settingFile)) 
     { 
      writeSettings(); 
     } 
     FileStream fileStream = new FileStream(settingFile, FileMode.Open, FileAccess.Read, FileShare.None); 
     BinaryFormatter bf = new BinaryFormatter(); 
     bf.Deserialize(fileStream); 
     fileStream.Close(); 
    } 

實際的錯誤味精:沒有找到相關地圖對象「822476800」 。這發生在這條線上:

bf.Deserialize(fileStream); 
+0

您可以複製確切的錯誤信息? –

+1

「教程」。什麼教程?請鏈接。 – Oded

+4

@明確提供了互聯網上唯一可用的教程。 「 – CodesInChaos

回答

0

我弄清楚是什麼錯誤對於那些在未來誰可能是敲打他們的頭想知道什麼地方出了錯。這其實很簡單。實際上是一個小錯字。太糟糕了,M $有可怕的錯誤消息,真的不告訴你的錯誤可能發生:

只需更換這行:

public void GetObjectData(SerializationInfo info, StreamingContext context) 
{ 
    Type myTypeObj = Type.GetType("Settings"); 
    foreach (FieldInfo p in myTypeObj.GetFields()) 
    { 
     Object value = p.GetValue(null); 
     info.AddValue(p.Name, value, p.GetType()); 
    } 
} 

有了這個:

public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     Type myTypeObj = Type.GetType("Settings"); 
     foreach (FieldInfo p in myTypeObj.GetFields()) 
     { 
      Object value = p.GetValue(null); 
      info.AddValue(p.Name, value, value.GetType()); 
     } 
    } 

而這它!所有序列化/反序列化都很好。你不可能猜出錯誤信息出錯的地方:*沒有對象'822476800'*的地圖。

注:在最後一行,p.GetType應該value.GetType

+0

這就是相同的代碼兩次! –

+0

仔細閱讀,它絕對不是相同的代碼。 – Bill

+0

現在我明白了,那個人就像捉迷藏一樣! –

1

我會在這個答案的前言這是一個壞主意。序列化被設計爲序列化一個對象實例,靜態字段不是該實例的一部分。

我相信,當你有一個自定義的序列化程序,你需要以靜態前言對象名稱。。例如,名爲A的公共靜態成員需要添加爲static.A

這裏有一個鏈接,應該幫助:http://forums.codeguru.com/showthread.php?t=411604