In a webpage, I am storing a Dictionary
in the ViewState
:
Dictionary<string, string> items = new Dictionary<string, string>();
ViewState["items"] = items;
It works great. However, for some reasons, I want to use my own class instead of using Dictionary
directly :
class MyDictionary : Dictionary<string, string>
{
...
}
MyDictionary items = new MyDictionary();
ViewState["items"] = items;
It doesn't work as expected. ASP.NET complains about the fact
MyDictionary is not marked serializable
It's ok since class attributes are not herited. So I changed my code :
[Serializable]
class MyDictionary : Dictionary<string, string> { ... }
But if I do so, I get another error message, this time after page postback :
The state information is invalid for this page and might be corrupted.
System.Web.UI.ObjectStateFormatter.Deserialize(Stream inputStream)
If viewstate can serialize a Dictionary
, why it doesn't work for a class that inherit from it ? How to make this work?