views:

70

answers:

3

I'm having some trouble figuring out how to deserialize a binary file. I mostly can't figure out how to use the second argument of SerializationInfo.GetValue(); - if I just put a type keyword there, it's invalid, and if I use the TypeCode, it's invalid as well. This is my current attempt (obviously it doesn't build).

     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 );
  }
A: 
typeof(object)

or

instance.GetType()

where instance is an object. Of course, replace by the actual types in your case.

Jason
+3  A: 

The second argument in SerializationInfo.GetValue is the object type:

        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>));
            }
James
A: 

Instantiate temporary variables:

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

then in your lines below:

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

Hope this helps, Best regards, Tom.

tommieb75