2012-06-11 3 views
-3

나는 정확히 this tutorial을 온라인으로 따르지 만 어떻게 든 그것은 오류를 일으 킵니다. 오브젝트 맵이나 무언가가 없다고 말하면됩니다.C#은 바이너리로 serialize 할 수 없습니다

나는 내가 직렬화 할 정적 객체 다음이 '822476800'객체를지도 : 여기

[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(); 
    } 

실제 오류 MSG를 . 이이 라인에서 발생 :이 나쁜 생각과

bf.Deserialize(fileStream); 
+0

정확한 오류 메시지를 복사 할 수 있습니까? –

+1

"튜토리얼" 튜토리얼은 무엇입니까? 링크하십시오. – Oded

+4

인터넷에서 유일하게 제공되는 자습서를 분명히 읽으십시오. – CodesInChaos

답변

0

나는 오류가 무엇이 잘못되었는지 궁금 자신의 머리를 두드리는 될 수있는 미래의 사람들을 위해 무엇인지 알아낼. 사실 아주 간단합니다. 실제로 작은 오타. 이와

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()); 
     } 
    } 

을 그리고 그 너무 나쁜 M $으로 오류가 발생했을 수 있습니다 정말 당신을 말하지 않는 끔찍한 오류 메시지가 그것! 모든 것은 순조롭게 직렬화/비 직렬화합니다. 오류 메시지와 함께 어디서 잘못되었는지 추측 할 수 없습니다. * '822476800'* 객체에 대한지도가 없습니다.

참고 : 마지막 줄에서 p.GetType은 값이어야합니다. 유형

+0

는 동일한 코드를 두 번 사용합니다. –

+0

주의 깊게 읽으십시오. 분명히 동일한 코드가 아닙니다. – Bill

+0

나는 그것을 지금 본다. 사람은 숨기고 찾는다! –

1

나는이 대답을 서문 것이다. 직렬화는 객체 인스턴스를 직렬화하도록 설계되었으며 정적 필드는 해당 인스턴스의 일부가 아닙니다.

사용자 정의 시리얼 라이저가있는 경우 객체 이름 앞에 정적이 필요하다고 생각합니다.. 예를 들어 A라는 공개 정적 멤버는 static.A으로 추가해야합니다.

여기에 도움이 될 링크입니다 : http://forums.codeguru.com/showthread.php?t=411604

관련 문제