2013-04-02 23 views
6

Sto tentando di deserializzare un oggetto che ho salvato in un file (con formattatore binario). Qualunque cosa provo, ottengo l'eccezione: interruzione del flusso incontrato prima analisi è stata completatadeserialize del formatter: End of Stream rilevato prima del parsing completato

ho le seguenti righe di codice:

public static T DeserializeFromBinaryFile<T>(string fileName) 
{ 
    T instance = default(T); 
    FileStream fs = new FileStream(fileName, FileMode.Open); 
    try 
    { 
     BinaryFormatter formatter = new BinaryFormatter(); 
     instance = (T)formatter.Deserialize(fs); 
    } 
    catch{} 
    finally 
    { 
     fs.Close(); 
    } 

    return instance; 
} 

Ho anche provato:

public static T DeserializeFromBinaryFile<T>(string fileName) 
{ 
    T instance = default(T); 
    FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); 
    MemoryStream ms = new MemoryStream(); 
    try 
    { 
     byte[] bytes = new byte[fs.Length]; 
     fs.Read(bytes, 0, (int)fs.Length); 
     ms.Write(bytes, 0, (int)fs.Length); 
     ms.Position = 0; 
     ms.Seek(0, SeekOrigin.Begin); 
    } 
    catch { } 

    try 
    { 
     BinaryFormatter formatter = new BinaryFormatter(); 
     instance = (T)formatter.Deserialize(ms); 
    } 
    catch { } 
    finally 
    { 
     ms.Close(); 
     fs.Close(); 
    } 

    return instance; 
} 

Ma qualunque cosa io fare, ottenere sempre l'eccezione:

La fine del flusso rilevato prima dell'analisi è stata completata

AGGIUNTA: Ho appena effettuato alcuni test aggiuntivi. Se ho un oggetto semplice, solo alcune proprietà, funziona perfettamente. Usando un oggetto più grande (più grande), avendo altri oggetti, enumerati, ecc. Incapsulati, è allora che ottengo l'eccezione.

risposta

1

Tenta di impostare il position-0 per il flusso, all'interno del secondo blocco try/catch:

BinaryFormatter b = new BinaryFormatter(); 
s.Position = 0; 
return (YourObjectType)b.Deserialize(s); 
+1

ho già fatto, non aiuta, dà ancora l'eccezione. – royu

0

Hai provato a deserializzare il filestream piuttosto che il MemoryStream? funziona per me.

FileStream fs = new FileStream(fileName, FileMode.Open); 
fs.position=0; 
instance = (T)formatter.Deserialize(fs); 

riguarda

0

Ogni membro all'interno della classe da serializzare, devono avere l'attributo [Serializable()] o essere marcati [NonSerialized]. Nota, enumerazione e tutti i tipi semplici incorporati sono già serializzabili e non richiedono l'attributo [Serializable()].

L'esempio seguente riporta lo stesso errore. Decommentare [Serializable()] sopra public class B e la deserializzazione si verificherà senza errori.

* Non è possibile dire con certezza se questa sia la causa del tuo errore, perché non hai fornito un campione della classe risultante nell'errore.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Runtime.Serialization; 
using System.Runtime.Serialization.Formatters.Binary; 

//[Serializable()] 
public class B 
{ 
    public int y = 2; 
} 
[Serializable()] 
public class A 
{ 
    public int x = 2; 
    /*[NonSerialized]*/ public B b; 
    public static T DeserializeFromBinaryFile<T>(string fileName) 
    { 
     T instance = default(T); 
     FileStream fs = new FileStream(fileName, FileMode.Open); 
     try 
     { 
      BinaryFormatter formatter = new BinaryFormatter(); 
      instance = (T)formatter.Deserialize(fs); 
     } 
     catch (System.Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
     } 
     finally 
     { 
      fs.Close(); 
     } 

     return instance; 
    } 
    public static void SerializeBinaryFile<T>(string fileName,T t) 
    { 
     try 
     { 
      using (FileStream fs = File.Open(fileName, FileMode.Create)) 
      { 
       BinaryFormatter bf = new BinaryFormatter(); 
       bf.Serialize(fs, t); 
      } 
     } 
     catch (System.Exception ex) 
     { 

     } 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     A testA = new A(); 
     A.SerializeBinaryFile("test.dat", testA); 
     A testReadA = A.DeserializeFromBinaryFile<A>("test.dat"); 
    } 
} 

https://msdn.microsoft.com/en-us/library/ms973893.aspx