2012-06-11 63 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