Last post Jun 24, 2013 01:22 PM by suryaphani
Jun 20, 2013 07:43 PM|suryaphani|LINK
We have a custom mechanism to compress view state and for that we have a custom class which inherits from "IStateFormatter" and implements Serialize & Deserialize method. The issue comes when i try to assign the output from the
custom class to the State Formatter object in PageStatePersister class. The error is as below -
<div>"Object of type [MyCustomFormatter]cannot be converted to type System.Web.UI.IStateFormatter2"</div>
var formatter = new MyCustomFormatter(base.StateFormatter);
var field = typeof(PageStatePersister).GetField("_stateFormatter", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(this, formatter); [ This is the line which throws the exception above ]
The code used to work correctly in dotnet 4.0. MyCustomFormatter class is of type IStateFormatter.
Jun 20, 2013 11:23 PM|Chunyan Yu - MSFT|LINK
ObjectStateFormatter Class is serializes and deserializes object graphs that represent the state of an object. This class cannot be inherited.
ObjectStateFormatter is used by the
PageStatePersister class and classes that derive from it to serialize view state and control state. It is also used by the
LosFormatter class to provide object state graph formatting for various parts of the ASP.NET infrastructure. while conventional string types and string arrays are written to and from a serialized binary writer unaltered, some strings are optimized
by creating internal string tables. Strings are optimized using these tables if the string has an associated
TypeConverter object or if the string is actually an instance of the
IndexedStringclass. Other types not listed above are binary-serialized using a
BinaryFormatter object if they implement the ISerializable interface or are decorated with the
SerializableAttribute attribute. The ObjectStateFormatter class is not optimized for any of these serializable types.
More details please reference:
Hope that's helpful.
Jun 24, 2013 01:22 PM|suryaphani|LINK
Thanks Chunyan, but it does not answer why we are getting the casting error and what is IStateFormatter2. Please note this was working fine in framework 4.0. It is only 4.5 upgrade that broke it.