I have a class like this that I want to save in ViewState for an asp.net UserControl. It basically extends a generic list with a few methods but also adds some properties, e.g.
public class MyListClass: List<MyObject>
{
public string ExtraData;
// some public methods
}
MyObject is serializable, with methods implemented like this:
[Serializable()]
class MyObject
{
public MyObject(SerializationInfo info, StreamingContext ctxt)
{
Prop1 = (string)info.GetValue("Prop1", typeof(string));
//...
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("Prop1",Prop1);
}
}
And generic lists are inherently serializable. ViewState works fine with List<MyObject> but I can’t seem to figure out how to implement serialization for MyListClass.
Intuitively what I want to do is something like this:
public MyListClass(SerializationInfo info, StreamingContext ctxt)
{
ExtraData= (string)info.GetValue("ExtraData", typeof(string));
this = (List<MyClass>)info.GetValues("BaseList",typeof(List<MyClass>));
}
Obviously that won’t work. What is the right way to do this?
You should just be able to put the Serializable tag on MyListClass, which will cause ExtraData to be serialized as its own field.