2014-03-03 47 views
11

我試圖進行ISerializable,並在此難住。我做了兩個具有「Serializable」屬性的類。只有一個類派生自ISerializable,併爲其定義了GetObjectData。讓我們調用這個類A.另一個不是從那裏通過GetObjectData派生出來的ISerializable沒有爲它定義。讓我們把這個類叫做B.我沒有爲類A提供任何特殊的構造函數。現在在運行時類A中顯示錯誤,如「Special constructor is missing」。兩個類的語法都是相同的。所以,錯誤可能是一些其他的事情,但它不應該是關於構造函數。否則,我也應該得到類B的相同錯誤。請參閱下面的代碼。有人可以說它背後的原因嗎?
注:我使用Windows 7 - 64位與Visual Studio 2010反序列化類型T的對象的構造函數未找到

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

namespace Delete_This 
{ 
    [Serializable] 
    class After_Implementing_ISerializable:ISerializable 
    { 
     int a; 
     string b; 
     public After_Implementing_ISerializable(int a, string b) 
     { 
      this.a = a; 
      this.b = b; 
     } 
     public void GetObjectData(SerializationInfo info, StreamingContext context) 
     { 

     } 
     public void Check() 
     { 
      After_Implementing_ISerializable s = new After_Implementing_ISerializable(15, "100"); 
      FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate); 

      BinaryFormatter bf = new BinaryFormatter(); 
      bf.Serialize(fs, s); 
      fs.Close(); 

      fs = new FileStream("temp.xml", FileMode.OpenOrCreate); 
      After_Implementing_ISerializable d = (After_Implementing_ISerializable)bf.Deserialize(fs); 
      fs.Close(); 
     } 
    } 

    [Serializable] 
    class Without_Implementing_ISerializable 
    { 
     int a; 
     string b; 
     public Without_Implementing_ISerializable(int a,string b) 
     { 
      this.a = a; 
      this.b = b; 
     } 

     public void Check() 
     { 
      Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(15, "100"); 
      FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate); 

      BinaryFormatter bf = new BinaryFormatter(); 
      bf.Serialize(fs, s); 
      fs.Close(); 

      fs = new FileStream("temp.xml", FileMode.OpenOrCreate); 
      Without_Implementing_ISerializable d = (Without_Implementing_ISerializable)bf.Deserialize(fs); 
      fs.Close(); 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
     Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(5,"Five"); 
      s.Check(); 

      After_Implementing_ISerializable s1 = new After_Implementing_ISerializable(6, "Six"); 
      s1.Check(); 
     } 
    } 
} 

這是我

"The constructor to deserialize an object of type 'Delete_This.After_Implementing_ISerializable' was not found."} 
+0

@Raymond謝謝你對主題的很好的修改 – prabhakaran

回答

24

的錯誤是,你需要實現類型序列化的構造實施ISerializable;該序列化構造函數負責使用GetObjectData方法對序列化進行反序列化。

序列化構造函數將如下所示,其中第一個參數SerializationInfo和第二個參數StreamingContext

protected ClassName(SerializationInfo info, StreamingContext context) 
{ 

} 

提供的鏈接中的備註部分談論此主題。

ISerializable接口意味着構造函數的簽名爲 構造函數(SerializationInfo信息,StreamingContext上下文)。 在反序列化時間,當前的構造函數僅在 之後被調用,SerializationInfo中的數據已被 格式化程序反序列化。一般來說,如果 類沒有密封,這個構造函數應該受到保護。

+0

我同意你的建議來描述一個構造函數。我的問題是在兩個類Check()函數具有相同的語法。在兩個地方看到對象d。那麼運行時間如何只顯示一個錯誤。接口不能實現任何關於構造函數的東西。錯誤是關於構造函數。這是讓我困惑的一點。 – prabhakaran

+0

@prabhakaran我錯過了什麼嗎?你的第二個類沒有實現'ISerializable'接口,所以這個構造函數不是必需的。這對於實現'ISerializable'接口的類型是強制性的 –

+0

@SriramCan你只需要放一些代碼來解釋第一行。我認爲這一點可以清除所有的東西.- – prabhakaran

相關問題