C#中的会话更新问题 [英] Session Update Issue in C#
本文介绍了C#中的会话更新问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从会话中获取对象列表,然后在该情况下更新该对象列表会话值自动更新。
这是一段代码: -
Hi,
I am getting List of Object from Session, then Update that list of object in that case session value is automatically updated.
This is the piece of code :-
if(Session["StudentData"]!=null)
{
List<Student> testStudent = Session["StudentData"] as List<Student>;
Response.Write("<br>Value in Session <br/>");
DisplayData(testStudent);
int i = 0;
foreach (Student obj in testStudent)
{
if (i == 2)
{
obj.Name = "Adi";
obj.Age = 23;
break;
}
i = i + 1;
}
Response.Write("<br>Modify Value in Session <br/>");
DisplayData(testStudent);
Response.Write("<br>Value in Session <br/>");
DisplayData(Session["StudentData"] as List<Student>);
}
如何避免这种情况?
为什么会这样?请详细解释。
等待你的回复。
谢谢。
How to avoid this ?
Why this happens ? Please explain in detail.
Waiting for your Reply.
Thanks.
推荐答案
而不是更新相同的列表,为什么不自己复制它。
instead of updating the same list why not take a copy of it yourself.
if(Session["StudentData"]!=null)
{
List<Student> testStudent = Session["StudentData"] as List<Student>;
Response.Write("<br>Value in Session <br/>");
DisplayData(testStudent);
int i = 0;
List<Student> another;
foreach (Student obj in testStudent)
{
another[i] = obj;
if (i == 2)
{
another[i].Name = "Adi";
another[i].Age = 23;
break;
}
i = i + 1;
}
Response.Write("<br>Modify Value in Session <br/>");
DisplayData(another);
Response.Write("<br>Value in Session <br/>");
DisplayData(Session["StudentData"] as List<Student>);
}
会话已更新,因为变量testStudent等于Session [StudentData]中的列表。这是你代码中的第一个陈述:List< student> testStudent = Session [StudentData]作为List< student> ;;
请记住这些都是引用类型。所以testStudent == Session [StudentData]。 Session [StudentData]中的所有对象都在testStudent中。
如果你的目标是更改testStudent列表中的对象而不更改会话,那么你必须先克隆对象,然后再将它们添加到新的列表testStudent列表中;
为了实现这一目标,您将从ICloneble中获取Student类,并将以下扩展类添加到你是项目。
The session is updated because the variable testStudent is equal to the list in Session["StudentData"]. That is the first statement in you're code : List<student> testStudent = Session["StudentData"] as List<student>;
Remember these are all reference types. So testStudent == Session["StudentData"]. Equally all objects in Session["StudentData"] are in testStudent.
When it is you're goal to change the objects in testStudent list without changing the session, you must clone the objects before add them to a new list, the testStudent list;
To achieve that derive you're Student class from ICloneble and add the following extension class to you're project.
static class Extensions
{
public static IList<t> Clone<t>(this IList<t> listToClone) where T: ICloneable
{
return listToClone.Select(item => (T)item.Clone()).ToList();
}
}
</t></t></t>
您的代码中的第一个语句可以是:
the first statement in you code can the be:
IList<student> testStudent = Session.Clone<student>();
</student></student>
供将来参考:
在这样的情况下,通常首选复制构造函数使用存储在会话中的对象而不更新会话中的对象。
@MSDN如何:编写复制构造函数
由于缺乏指定的克隆深度(深度与浅度),克隆在大多数情况下不是首选。
在您的情况下,带有复制构造函数的集合类或
For future reference:
In instances like this, a copy constructor is typically preferred when working with objects stored in session without updating the object in session.
@MSDN How to: Write a Copy Constructor
Cloning isn't preferred in the majority of situations due to lack of specified cloning depth (deep vs shallow).
In your case, a collection class with a copy constructor or
.ToList<t>();</t>
可用于获取副本。
could be used to obtain a copy.
var studentsInSession = Session["Student"] as List<Student>
var students = studentsInSession.ToList();
students.Add(new Student("John", "Smith"));
这将允许修改列表而不反映会话的更改。
This would allow the modification of the list without reflecting the change to the session.
这篇关于C#中的会话更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文