我们可以将哪些类型的对象置于视图状态? [英] Which types of objects can we place in view state?

查看:40
本文介绍了我们可以将哪些类型的对象置于视图状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我们必须设置serializable属性以将对象保存为视图状态.

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?

推荐答案

ViewState 使用

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

ObjectStateFormatter类是优化以序列化和格式化许多常见的.NET Framework参考类型,以及常量以下表格列出了优化.

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(无),String.Empty,Byte,哈希表,配对,三元组,字符,HybridDictionary,Single,Type,Color,IDictionary,

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

另外,常规的字符串类型和字符串数组与序列化的二进制编写器保持不变,通过创建一些字符串来优化内部字符串表.字符串是如果字符串具有关联的TypeConverter对象,或者该字符串实际上是一个IndexedString类的实例.

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.

上面未列出的其他类型是二进制序列化使用BinaryFormatter对象(如果他们)实现ISerializable接口或装饰有SerializableAttribute属性.这ObjectStateFormatter类不是针对任何这些进行了优化可序列化的类型.

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.

如果ObjectStateFormatter类遇到非可序列化,一个ArgumentException引发异常.

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

对于要在 ViewState 中进行二进制序列化的对象,需要使用 [可序列化] 属性.因此,您可以在 ViewState 中放入具有此属性的任何对象.请注意,诸如string,int,float等简单类型也可以放置在 ViewState 中.

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.

这篇关于我们可以将哪些类型的对象置于视图状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆