CollectionBase and ISerializable

  • Thread starter Thread starter Sahil Malik
  • Start date Start date
S

Sahil Malik

Well, the problem is pretty simple.

I have a class that inherits from CollectionBase, but Implements
ISerializable. The name of this class is UsageLogs .. so now my UsageLogs
can contain multiple UsageLog.

Now when I try to serialize this class it works

public void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("InnerList", InnerList) ;

}



But when I try to deserialize it,



public UsageLogs(SerializationInfo info, StreamingContext context)

{

ArrayList innerList = (ArrayList) info.GetValue("InnerList",
typeof(ArrayList)) ;


foreach(UsageLog log in innerList)

{

List.Add(log) ;

}

// this.InnerList.AddRange((ArrayList) info.GetValue("InnerList",
typeof(ArrayList))) ;

}



I have gotten mixed results viz -

a) List is readonly.

b) It won't let me add the parameters manually.

c) it just wouldn't get any parameters at all ....



Please Help !!!!

PS: I cannot get away with <Serializable()> since I have public delegates in
this class.

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/
 
Hey Sahil

This will not work because the objects contained in your collection are not fully constructed by the time the deserialization constructor of the collection is called. You need to implement the interface IDeserializationCallback and add the items to the collection in the implemention of IDeserializationCallback.OnDeserialization to make this work. I recommend reading Jeffrey Richter's article on how the deserialization of hashtables is implemented

http://msdn.microsoft.com/msdnmag/issues/02/07/net

Regards, Jakob.
 
Thanks Jakob. You are right, it didnt' work. I ended up implementing
ICollection and using a HashTable inside instead .. that gave me the same
functionality.

_ SM

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/



Jakob Christensen said:
Hey Sahil,

This will not work because the objects contained in your collection are
not fully constructed by the time the deserialization constructor of the
collection is called. You need to implement the interface
IDeserializationCallback and add the items to the collection in the
implemention of IDeserializationCallback.OnDeserialization to make this
work. I recommend reading Jeffrey Richter's article on how the
deserialization of hashtables is implemented:
 
Back
Top