加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 19:50:10 所属栏目:asp.Net 来源:网络整理
导读:我想知道为什么我们必须设置serializable属性来保存视图状态中的对象. 另外,我们可以在视图状态中存储哪种类型的对象? 解决方法 使用 ObjectStateFormatter使用二进制序列化序列化ViewState.来自文档的引用: The ObjectStateFormatter class is optimized
我想知道为什么我们必须设置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中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读