I have a few custom controls, inherited from CompositeControl. I implement ISerializable for each control. This is one example:
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("ID"),
ToolboxData("<{0}:EddRadioButtonList runat=\"server\"> </{0}:EddCheckBox>"),
Serializable
]
public class EddRadioButtonList : CompositeControl, IChannelControl,ISerializable
{
I have a custom collection of these control. I also mark [Serializable]:
[Serializable]
public class EddControlCollection : CollectionBase
{
....
}
In my main class, I serialize and deserialize the collection of controls:
public void Serialize()
{
Stream s = File.Open("EDDTemp.xml", FileMode.Create);
//SoapFormatter formatter = new SoapFormatter();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(s, _EDDControls);
s.Close();
}
public void Deserialize()
{
if (File.Exists("EDDTemp.xml"))
{
Stream s = File.Open("EDDTemp.xml", FileMode.Open);
//SoapFormatter formatter = new SoapFormatter();
BinaryFormatter formatter = new BinaryFormatter();
_EDDControls = (EddControlCollection)formatter.Deserialize(s);
//object o = formatter.Deserialize(s);
s.Close();
}
}
I have an error when I call Deserialize( ). It complained that the constructor to deserialize an object of type '....EddSubmitButton' was not found. EddSubmitButton is one of my custom control. How can I deserialize the custom controls properly?
I'm thinking I only want to serialize enough information to recreate the controls on the fly, but I seem to have a problem.
I figured out my own issue. I thought I post it here, in case somebody has the same issue.
I have to implement the deserialization constructor for each control because formatter.Deserialize() will call the constructor of each control in the collection. The deserialization constructor looks like this:
//deserialization constructor
public EddRadioButtonList(SerializationInfo info, StreamingContext context)
{
....
}
Most web sites tell you to implement ISerializable, but they don't tell you to implement the deserialzation constructor. You need this constructor for deserialization to work.
Bookmarks