Deserialization Problems

While prototyping something today, I had an object that I wanted to be able to serialize out to disk. This object was derived from Hashtable, which already implemented the ISerializable interface so I thought I was all set. Serialization worked fine. Then I went to deserialize it and I got the following error:

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

I was slightly confused until I looked at the documentation for the Hashtable's constructor. The constructor for deserializing was protected. I needed to make a public constructor and call the base class' constructor:

public MyClass(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
Published Thursday, January 29, 2004 11:04 PM by PSteele

Comments

# re: Deserialization Problems

I wasn't aware it needed to be public, just that you had to have one.

Does the following not work?

protected MyClass(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

Friday, January 30, 2004 8:58 AM by Sam Meldrum

# Cont'd: Deserialization Problems

Friday, January 30, 2004 9:57 AM by TrackBack

# Serialization and Inheritence

Friday, January 30, 2004 9:40 PM by TrackBack

# re: Deserialization Problems

You have solved my problem by using a quick search using Google. Thanks!

Friday, March 19, 2004 10:41 PM by Mark Wagner

# re: Deserialization Problems

yes, "protected" works as well.

Wednesday, April 28, 2004 10:12 AM by semaphor

# re: Deserialization Problems

Many thanks. Your comment really pulled me out of a soup :-) ...

Friday, July 23, 2004 8:15 AM by Kumudu Widyaratne

# re: Deserialization Problems

Great article. Helped me out of a jam. :)

Friday, June 15, 2007 1:56 PM by Gabe

# re: Deserialization Problems

I googled it too.  Thanks so much!

Wednesday, June 20, 2007 3:28 PM by Peter Wakeley

# re: Deserialization Problems

fantastic - big problem, 2 mins to solve

Tuesday, June 26, 2007 2:03 PM by chris