2009-11-24 100 views
0

我在計算如何反序列化二進制文件時遇到了一些麻煩。我大多無法弄清楚如何使用SerializationInfo.GetValue()的第二個參數; - 如果我只是在那裏輸入一個類型關鍵字,那麼它是無效的,如果我使用TypeCode,它也是無效的。這是我目前的嘗試(顯然它不會構建)。如何對二進制文件進行反序列化

 protected GroupMgr(SerializationInfo info, StreamingContext context) { 
     Groups = (Dictionary<int, Group>) info.GetValue("Groups", (Type) TypeCode.Object); 
     Linker = (Dictionary<int, int>) info.GetValue("Linker", (Type) TypeCode.Object); 
     } 

回答

3

在SerializationInfo.GetValue第二個參數是對象類型:

 protected GroupMgr(SerializationInfo info, StreamingContext context) { 
      Groups = (Dictionary<int, Group>) info.GetValue("Groups", typeof(Dictionary<int, Group>)); 
      Linker = (Dictionary<int, int>) info.GetValue("Linker", typeof(Dictionary<int, int>)); 
      } 
0
typeof(object) 

instance.GetType() 

其中instanceobject。當然,取而代之的是你的案例中的實際類型。

0

實例化的臨時變量:

 
Dictionary<int, Group> tmpDict = new Dictionary<int, Group>(); 
Dictionary<int, int> tmpLinker = new Dictionary<int, int>(); 

然後在下面的幾行:

 
Groups = (Dictionary<int, Group>) info.GetValue("Groups", tmpDict.GetType()); 
Linker = (Dictionary<int, int>) info.GetValue("Linker", tmpLinker.GetType()); 

希望這有助於, 問候, 湯姆。

相關問題