asp.net – 我们可以在视图状态中放置哪些类型的对象?

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

另外,我们可以在视图状态中存储哪种类型的对象?

解决方法

使用
ObjectStateFormatter使用二进制序列化序列化ViewState.来自文档的引用:

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.

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

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。