OK, if I have a class like this …
[serializable]
public class MyClass() : ISerializable
{
public Dictionary<string, object> Values {get; set;}
}
I know what I have to do to serialize it (the answer, for those trying to find a quick answer, is this)…
protected MyClass(SerializationInfo info, StreamingContext context)
{
Values = (Dictionary<string, object>)info.GetValue("values", typeof(Dictionary<string, object>));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("values", Values);
}
My question is what do I do if, instead, I wanted to defined a class that inherited from Dictionary?
I get this far…
[serializable]
public class MyClass() : Dictionary<string, object>, ISerializable
{
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("me", this);
}
}
but then I get lost. I can’t write this …
protected MyClass(SerializationInfo info, StreamingContext context)
{
this = (MyClass)info.GetValue("me", typeof(MyClass));
}
‘cos ‘this’ is r/o. So, how would I proceed? Am I even right about the implementation of GetObjectData()?
I don’t believe it will make a difference, but just in case it does, I’m writing this under .Net 4.0
Dictionary<T, V>already implementsISerializable(see this). So just call the methods in your base class: