Which types of objects can we place in view state?

跟風遠走 提交于 2020-01-02 04:13:25

问题


I want to know why we must set the serializable attribute to save an object in view state.

Also, which type of objects can we store in view state?


回答1:


ViewState is serialized using binary serialization using ObjectStateFormatter. Quote from the documentation:

The ObjectStateFormatter class is optimized to serialize and format many common .NET Framework reference types, as well as constants. The following table lists the types that are optimized.

Array, DateTime, Int16, String, ArrayList, Double, Int32, String [], Boolean, Enum, null (Nothing), String.Empty, Byte, Hashtable, Pair, Triplet, Char, HybridDictionary, Single, Type, Color, IDictionary,

Additionally, 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 IndexedString class.

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.

If the ObjectStateFormatter class encounters a type that is not serializable, an ArgumentException exception is thrown.

For an object to be binary serializable in the ViewState it needs to be decorated with the [Serializable] attribute. So you can put in ViewState any object that has this attribute. Note that simple types like string, int, float, ... can also be placed in ViewState.




回答2:


Types must be serializable to be placed in ViewState. ViewState is a serialized collection of objects so any serializable objects may be put in there.




回答3:


What objects - as per Darren and Andrew - any serializable ones. Why Serializable? So that the state can be serialized like

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NTgzODUwMg9kFgJmD2QWAgIDD2QWAgIBD2QWBgIBDw8WAh4EVGV4dAUXU3VwcGxpZXIgUG9y

However in practice, need to keep viewstate to a minimum as it has bandwidth / page size performance implications. Controls are the most common users of viewstate.




回答4:


Don't put objects of type Guid in ViewState. The ObjectStateFormatter does not recognize them and they get jammed in using the BinaryFormatter at a whopping 130bytes per GUID. You're better off storing them as strings and parsing them on reading. This doesn't always work, however, for instance when using Guid as a key in a grid.

Also, when loading/saving enumerations to the ViewState, you'll gain a bit by converting them from/to an integral type. In your control's property you know the type of enumeration required, and that doesn't need to be encoded in the serialized ViewState.

It pays to use Reflector to check out the implementation of ObjectStateFormatter to see how everything is handled, so that you have some understanding of the process and its limitations.




回答5:


Pretty much anything that is serializable. However, keep in mind that the data will be sent over the wire between the server and the web browser (and back, during postbacks), so you may want to keep the volume down, if possible.



来源:https://stackoverflow.com/questions/3583573/which-types-of-objects-can-we-place-in-view-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!