Asp.net - 对会话对象的更改是否仍然存在?

时间:2022-02-02 16:54:44

I'm using the session to hold a custom object UserSession and I access the session like this:

我正在使用会话来保存自定义对象UserSession,我像这样访问会话:

UserSession TheSession = HttpContext.Current.Session["UserSession"] as UserSession;

Then, in my code, I modify a property of TheSession like this

然后,在我的代码中,我像这样修改了TheSession的属性

TheSession.Prop1 = some new value;

My question is this: when I change the value, does it change the value inside the session that's in HttpContext.Current.Session["UserSession"] or just the TheSession variable, in which case I'd need to reassign the object variable to the session.

我的问题是:当我更改值时,它是否更改HttpContext.Current.Session [“UserSession”]中的会话内的值或仅更改TheSession变量,在这种情况下,我需要将对象变量重新分配给会议。

Thanks.

4 个解决方案

#1


4  

The standard session provider is a simple dictionary that stores objects in memory.

标准会话提供程序是一个将对象存储在内存中的简单字典。

You're modifying the same object that you put in the session, so the modifications will persist.
(unless you're using an evil mutable struct)

您正在修改放在会话中的同一对象,因此修改将持续存在。 (除非你使用邪恶的可变结构)

#2


4  

Yes it is the same object. It is very easy to see in the following example:

是的,它是同一个对象。在以下示例中很容易看到:

Session["YourObject"] = yourObject;
yourObject.SomeProperty = "Test";
var objectFromSession = (YourObjectType)(Session["YourObject"]);
Response.Write(objectFromSession.SomeProperty);

The Write will display: Test

写入将显示:测试

#3


1  

create a static class with static properties:

使用静态属性创建一个静态类:

public static class UserSession
{
    public static int UserID
    {
         get { return Convert.ToInt32(Session["userID"]); }
         set { Session["userID"] = value; }
    }
}

When u use it:

当你使用它时:

UserSession.UserID = 23;
Response.Write(UserSession.UserID);

it will use the session variable to store information passed to this property.

它将使用会话变量来存储传递给此属性的信息。

#4


0  

It doesn't need to reassign the object. Because both of them are pointing to same instance in memory

它不需要重新分配对象。因为它们都指向内存中的同一个实例

#1


4  

The standard session provider is a simple dictionary that stores objects in memory.

标准会话提供程序是一个将对象存储在内存中的简单字典。

You're modifying the same object that you put in the session, so the modifications will persist.
(unless you're using an evil mutable struct)

您正在修改放在会话中的同一对象,因此修改将持续存在。 (除非你使用邪恶的可变结构)

#2


4  

Yes it is the same object. It is very easy to see in the following example:

是的,它是同一个对象。在以下示例中很容易看到:

Session["YourObject"] = yourObject;
yourObject.SomeProperty = "Test";
var objectFromSession = (YourObjectType)(Session["YourObject"]);
Response.Write(objectFromSession.SomeProperty);

The Write will display: Test

写入将显示:测试

#3


1  

create a static class with static properties:

使用静态属性创建一个静态类:

public static class UserSession
{
    public static int UserID
    {
         get { return Convert.ToInt32(Session["userID"]); }
         set { Session["userID"] = value; }
    }
}

When u use it:

当你使用它时:

UserSession.UserID = 23;
Response.Write(UserSession.UserID);

it will use the session variable to store information passed to this property.

它将使用会话变量来存储传递给此属性的信息。

#4


0  

It doesn't need to reassign the object. Because both of them are pointing to same instance in memory

它不需要重新分配对象。因为它们都指向内存中的同一个实例