Thursday 17 July 2008

"The constructor to deserialize an object of type 'MyGenericCollection' was not found."

This exception is thrown by an application that tries to deserialize an instance of MyGenericCollection. In my case it was thrown by BizTalk that stores current state of it variables by their serialization and saving into its database. Then it restores the previous state by deserialization.
MyGenericCollection is a class that derives from Dictionary<Key,value>, which is serializable.

So why the deserialization failed?
Because the deserialization is done within constructor which accepts SerializationInfo and StreamingContext, but constructors are not derived. It means that we need to to define such constructor:

public class MyGenericCollection : Dictionary<Key,Value>
{
protected MyGenericCollection(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}

No comments: