2016-07-17 26 views
1

我有一個名爲Schematic的類,它存儲了用於我的遊戲的塊的結構。我試圖找到一種方法來使用BinaryFormatter保存和加載它們,但是我遇到了反序列化問題。當我反序列化時,我無法投射到我的源類型,而只能讓我得到一個字段,一個整數的二維數組。Deserializtion返回類的字段而不是類

下面是示意圖類的代碼:

[Serializable] 
public class Schematic 
{ 
    public static Schematic BlankSchematic = new Schematic("BLANK"); 
    public int[,] Blocks; 
    public V2Int Size; 
    public V2Int Location = V2Int.zero; 

    public string Name; 

    //---PROPERTIES--- 
    //lower is more rare 
    public int Rarity = 100; 
    //---END PROPERTIES--- 

    public Schematic(string name) 
    { 
     Name = name; 
    } 
    public Schematic(string name, int[,] blocks) 
    { 
     Name = name; 
     ModifyBlockArray(blocks); 
    } 
    public void ModifyBlockArray(int[,] newBlocks) 
    { 
     Blocks = newBlocks; 
     Size = new V2Int(newBlocks.GetLength(0), newBlocks.GetLength(1)); 
    } 
} 

而且我的方法在一個單獨的類序列化和反序列化:

public void SaveSchematic(Schematic schem) 
{ 
    using (Stream stream = new FileStream(SchematicsDirectory + "/" + schem.Name + ".schem", FileMode.Create, FileAccess.Write, FileShare.None)) 
    { 
     BinaryFormatter bf = new BinaryFormatter(); 
     Debug.Log(schem.GetType()); 
     bf.Serialize(stream, schem); 
    } 

} 

public void LoadSchematics(string dir) 
{ 
    BinaryFormatter bf = new BinaryFormatter(); 

    DirectoryInfo info = new DirectoryInfo(dir); 
    FileInfo[] fileinfo = info.GetFiles("*.schem"); 
    for (int i = 0; i < fileinfo.Length; i++) 
    { 
     FileStream fs = new FileStream(dir + fileinfo[i].Name, FileMode.Open); 
     object tempO = bf.Deserialize(fs); 
     Debug.Log(tempO + ", " + tempO.GetType()); 
     Schematic temp = (Schematic)tempO; 
     SchematicsByName.Add(temp.Name, temp); 
     Schematics.Add(temp); 
     print("Loaded Schematic: " + temp.Name); 
     fs.Close(); 
     fs.Dispose(); 
    } 
} 

這是因爲當我尋找到一個序列化的文件很奇怪,我看到其他領域和類名「示意圖」。這裏是一個小例子文件:

ÿÿÿÿ   Assembly-CSharp  Schematic BlocksSizeLocationNameRarity System.Int32[,]V2Int V2Int    V2Int xy        
TestSavingd           

V2Int也被標記爲可序列化。當我反序列化時,我返回Blocks數組而不是整個類,這真的很奇怪。任何幫助將非常感激。

這是我在這裏的第一篇文章,很抱歉,如果我犯了錯誤。

+0

我看不出有什麼錯誤的地方我看,這是_ ** **非常好_對於第一個問題!做得好! - 我添加了Binary Serialization標籤,認爲可以很方便地指定已經在標籤中的序列化類型。 –

+0

請發佈V2Int類的定義 –

回答

0

我試着用一個樣本片斷,它爲我正確序列化和反序列化。請確保正在發送的原理圖對象具有所有正確的值。一般來說,反序列化的對象是最終值的最佳選擇。不要看序列化的文件。 另外,如果您的反序列化類型錯誤,則此行通常會引發拋出異常。

Schematic temp = (Schematic)tempO; 

因此,請執行以下代碼段並讓我們知道。 它對我來說很好。 (我做了一個隨機V2Int類基於它的構造函數)

public static string SchematicsDirectory = "d:\\temp\\s"; 

    static void Main(string[] args) 
    { 
     var p = new Program(); 

     var array = new int[2, 2]; 
     array[0, 0] = 1; 
     array[0, 1] = 2; 
     array[1, 0] = 3; 
     array[1, 1] = 4; 

     var testObject = new Schematic("fezzik", array); 

     p.SaveSchematic(testObject); 
     p.LoadSchematics(SchematicsDirectory + "/"); 
    } 

enter image description here

+0

不知道爲什麼它沒有工作更早,只是再次嘗試(沒有改變),現在它的工作原理。也許我的電腦只是瘋了或什麼的大聲笑。謝謝回覆。 – Xyag

+0

很酷。作爲一個額外的輸入,你不需要做fs.close,fs.dispose .. c#使用'using'關鍵字更容易.. auto-disposable .. using(var fs = new FileStream(dir + fileinfo [ i] .Name,FileMode.Open)){ object tempO = bf.Deserialize(fs); Debug.Log(tempO +「,」+ tempO.GetType());原理圖temp =(原理圖)temp0; SchematicsByName.Add(temp.Name,temp); Schematics.Add(temp);打印(「Loaded Schematic:」+ temp.Name); } –

相關問題