-
June 14th, 2010, 09:11 AM
#1
serialization and inheritance
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
{
...
#region ISerializable_implementation
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.FullTypeName = EDDQuestionID().ToString();
info.AddValue("ControlType", GetType().Name);
info.AddValue("ControlID", ID);
info.AddValue("Question", Question);
info.AddValue("Answer", EDDAnswerValue());
}
#endregion
...
}
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.
Thanks in advance.
-
June 14th, 2010, 02:20 PM
#2
Re: serialization and inheritance
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.
Happy coding
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|