SerializationException - "The constructor to deserialize an object of type 'foo' was not found." by Adobe ImageReady

Fri, 30 May 2008 23:13:00 +0400

SerializationException - "The constructor to deserialize an object of type 'foo' was not found."

by Adobe ImageReady @ Fri, 30 May 2008 23:13:00 +0400

Some classes be poor to be serializable. In line if you mode the SerializableAttribute to high sign it finished you may field into the affair this a SerializationException is buffaloed.

To part it you privation to position the another particulars in duplicate. Amid a on track paragon I'll formula a type named Foo. The things you scarcity to zero in betwixt rooming house are:

  1. Include the SerializableAttribute to the variety.
  2. Put away the sort fan the ISerializable interface.
  3. Solicitation sufficient privileges since your GetObjectData() implementation.

Amidst teaching you are rolling merchantry serialization. Together with here is how Foo would gamble on uninterrupted with thoroughly these within another:

using System.Runtime.Serialization;
using System.Security.Permissions;
...
[Serializable]
public class Foo : ISerializable {
protected Foo(SerializationInfo info,
StreamingContext context) {
_barString = info.GetString("_barString");
_barInteger = info.GetInt32("_barInteger");
}

[SecurityPermissionAttribute(
SecurityAction.Demand,
SerializationFormatter = true)]
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context) {
info.AddValue("_barString", _barString);
info.AddValue("_barInteger", _barInteger);
}
private string _barString;
private int _barInteger;
}
Note that the constructor is protected. This way it cannot be called except from the serialization code within .NET. It is however accessible by code in subclasses.

If your class is derived from a class that implements ISerializable, then you need to call the base class in both the constructor and the GetObjectData() implementation as follows:

[Serializable]
public class Foo : Base { // Base implements ISerializable
protected Foo(SerializationInfo info,
StreamingContext context)
: base(info, context) {
// your deserialization code
}

[SecurityPermissionAttribute(
SecurityAction.Demand,
SerializationFormatter = true)]
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context) {
base.GetObjectData(info, context);
// your serialization code
}
}
For more information please check MSFT's web site here.