如何存储和检索类和数组,在会话中列出 [英] How to store&retrieve a class and arrays, list in session

查看:52
本文介绍了如何存储和检索类和数组,在会话中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在会话中存储类,数组和List。然后在asp.net c#中从会话中检索它们。帮助人们!



什么我试过了:



给我一个例子。在asp.net c#。我不知道怎么做。多次google但是没有得到确切的结果。

解决方案

假设你有一个类元素数组:

 MyClass [] mca =  new  MyClass [ 100 ]; 
for int i = 0 ; i < 100 ; i ++)
{
mca [i ] = new MyClass(i);
}

然后将其存储到Session中是微不足道的:

会话[   MyArray] = mca; 

检索它并不复杂:

 MyClass [] mca = Session [MyArray] as MyClass []; 
if(mca!= null)
{
...
}

列表,数组,类实例 - 所有相同的技术。


列表obj =新列表();

obj.Add(1)



会话[会话] = obj;



并将类型转换回所需的列表。



同样可以完成对于任何复杂的对象。


I want to store class,arrays and List in session.Then retrieve them back from session in asp.net c#.Help guys!

What I have tried:

Give me an example guys.!In asp.net c#.I don't know how to do this.Googled many times but didn't get exact results.

解决方案

Assuming you have an array of your class elements:

MyClass[] mca = new MyClass[100];
for (int i = 0; i < 100; i++)
   {
   mca[i] = new MyClass(i);
   }

Then storing it into the Session is trivial:

Session["MyArray"] = mca;

And retrieving it is not complex either:

MyClass[] mca = Session["MyArray"] as MyClass[];
if (mca != null)
   {
   ...
   }

Lists, arrays, class instances - all the same technique.


List obj = new List();
obj.Add(1)

Session["Session"] = obj;

and typecast back to list where it requires.

same can be done for any complex object.


这篇关于如何存储和检索类和数组,在会话中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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