viewstate可以存储泛型 [英] viewstate can store generics

查看:73
本文介绍了viewstate可以存储泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在viewstate中存储泛型

解决方案

1.View状态是一个用于在回发之间存储临时数据的缓存。



2.根据给定的唯一密钥存储/获取数据。



3你可以在其中存储任何对象,即使该对象是使用泛型创建的,但是当你从缓存中获取数据时,你必须将它转换为真实类型,如下一个例子所示:

< pre lang =c#> // 假设您有一个通用列表
GenericList< T>我的列表;
myList = new new GenericList< MyClass>();
// ..填充列表的代码
// ...
// 将列表缓存到视图状态。
ViewState [ MyGenericList] = myList;
// ...
// 从视图状态获取缓存数据。
var myData =(GenericList< MyClass>)ViewState [ MyGenericList]; // 必须使用转化!


we can store generics in viewstate

解决方案

1.View state is a cache for storing temp data between postbacks.

2.The data are stored/get from there based on a given unique key.

3.You can store any object inside it, even that object is created by using generics, but when you get the data from the cache you must convert it to its real type like in the next example:

//Supposing that you have a generic list
GenericList<T> myList;
myList = new new GenericList<MyClass>();
//.. code to populate the list
//...
//Caching the list into the view state.
ViewState["MyGenericList"] = myList;
//...
//Getting the cached data from the view state.
var myData = (GenericList<MyClass>)ViewState["MyGenericList"]; //Must use conversion!


这篇关于viewstate可以存储泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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