2013-10-21 105 views
0

我有一個從二進制文件反序列化結構的問題。我的結構有另一種結構作爲財產:從二進制文件的結構成員反序列化

[Serializable()] 
public struct Record : ISerializable 
{ 
    public SensorInfo SensorInfo; 
    public List<short[]> Frames; 

    public Record(SerializationInfo info, StreamingContext ctxt) 
    { 
     this.Frames = (List<short[]>)info.GetValue("Frames", typeof(List<short[]>)); 
     this.SensorInfo = (SensorInfo)info.GetValue("SensorInfo", typeof(SensorInfo)); 
    } 

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt) 
    { 
     info.AddValue("Frames", this.Frames); 
     info.AddValue("SensorInfo", this.SensorInfo); 
    } 
} 

SensorInfo結構:

[Serializable] 
public struct SensorInfo : ISerializable 
{ 
    public double Frequency; 
    public int FramePixelDataLength; 
    public int FrameWidth; 
    public int FrameHeight; 

    public SensorInfo(SerializationInfo info, StreamingContext ctxt) 
    { 
     this.Frequency = (double)info.GetValue("Frequency", typeof(double)); 
     this.FramePixelDataLength = (int)info.GetValue("FramePixelDataLength", typeof(int)); 
     this.FrameWidth = (int)info.GetValue("FrameWidth", typeof(int)); 
     this.FrameHeight = (int)info.GetValue("FrameHeight", typeof(int)); 

    } 

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt) 
    { 
     info.AddValue("Frequency ", this.Frequency); 
     info.AddValue("FramePixelDataLength ", this.FramePixelDataLength); 
     info.AddValue("FrameWidth", this.FrameWidth); 
     info.AddValue("FrameHeight", this.FrameHeight); 
    } 
} 

我使用串行與此代碼:

static public class RecordSerializer 
{ 
    static RecordSerializer() 
    { 
    } 

    public static void SerializeRecord(string filename, Record recordToSerialize) 
    { 
     Stream stream = File.Open(filename, FileMode.Create); 
     BinaryFormatter bFormatter = new BinaryFormatter(); 
     bFormatter.Serialize(stream, recordToSerialize); 
     stream.Close(); 
    } 

    public static Record DeSerializeRecord(string filename) 
    { 
     Record recordToSerialize; 
     Stream stream = File.Open(filename, FileMode.Open); 
     BinaryFormatter bFormatter = new BinaryFormatter(); 
     bFormatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple; 
     recordToSerialize = (Record)bFormatter.Deserialize(stream); 
     stream.Close(); 
     return recordToSerialize; 
    } 
} 

系列化工作得很好,我得到一個輸出文件。但是,當我嘗試反序列化它時,我只從bFormatter.Deserialize和反序列化中得到異常失敗。

Frequency not found. 

流是不是空的,我檢查了這一點。

感謝您的任何想法。

+3

你改變第二'struct'保存該文件之後?另外,在'info.AddValue(「Frequency」,this.Frequency)中爲'Frequency'命名提供了額外的空間;' – MahanGM

+0

只是額外的空間全部是 –

回答

0

您正在爲屬性FrequencyFramePixelDataLength添加空白,如「Frequency」和「FramePixelDataLength」。

使用此:

info.AddValue("Frequency", this.Frequency); 
info.AddValue("FramePixelDataLength", this.FramePixelDataLength); 

代替:

info.AddValue("Frequency ", this.Frequency); 
info.AddValue("FramePixelDataLength ", this.FramePixelDataLength); 
+0

謝謝,現在它工作正常。 – PavelKumpan

相關問題