2009-11-24 52 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()); 

希望这有助于, 问候, 汤姆。

相关问题