存储在会话中的 C# DataTable 的多个实例..此引用如何工作?

发布于 2024-11-29 09:46:26 字数 1685 浏览 0 评论 0原文

我在存储在 Session 中的 DataTable 以及从此 Session 检索它时的代码执行方面遇到问题。也许这不是问题,只是我错误地理解了实例和引用的真正工作原理。

我有一个 POCO 类 User ,其中存储登录用户的所有数据。 POCO 类如下所示:

public class User 
{
   public int UserID { get; set; }
   public string UserFirstName { get; set; }
   public string UserLastName { get; set; }
   public DataTable UserRights { get; set; }
}

用户登录后(在登录页面上),我创建一个 User 类的实例,并将登录时检索到的所有数据存储到 Session["User"] 中

User _user = new User();
_user = MyLogin.GetUser() // method returns filled User object with data from db
Session["User"] = _user; // I store it into session

然后在我的页面上,当我需要来自会话的用户权限数据(存储在数据表中)时,我会查看会话并检索数据表:

User _currUser = (User)Session["User"];
DataTable dt = new DataTable();
dt = _currUser.UserRights;

此代码工作正常,但是当我查看数据表用户权限的会话时,会出现一些奇怪的行为<强>同一件事多次页面。

示例:我在页面上有两个用户控件,在每个控件中我都会像上面的代码一样查看 DataTable 的会话。然后,如果我更改其中一个用户控件代码中的 DataTable 对象实例,例如:

dt.Columns.Remove("RoleID");

...此更改也会影响第二个用户控件中的实例(??)。如果给实例不同的名称,情况也是如此:

// FirstUserControl.ascx.cs
User _currUser1 = (User)Session["User"];
DataTable dt1 = new DataTable();
dt1 = _currUser1.UserRights;
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = new DataTable();
dt2 = _currUser2.UserRights; <--- dt2 is without column "RoleID" too!? 

有人可以清楚地向我解释为什么会发生这种情况以及实例和引用在这个示例中是如何工作的(看起来我没有正确理解它们)。我一直认为,如果我执行像上面这样的代码,我会得到 dt1dt2 DataTable 对象,它们在处理时不会有任何共同点他们。

I have a problem with DataTable stored in Session and code execution when retrieving it from this Session. Maybe this isn't a problem and is just me wrongly understanding how instances and references really work.

I have a POCO class User where I store all the data of logged in user. The POCO Class look like this:

public class User 
{
   public int UserID { get; set; }
   public string UserFirstName { get; set; }
   public string UserLastName { get; set; }
   public DataTable UserRights { get; set; }
}

Once the user is logged in (on login page) I create an instance of User class and I store all retrieved data from login into Session["User"]:

User _user = new User();
_user = MyLogin.GetUser() // method returns filled User object with data from db
Session["User"] = _user; // I store it into session

Then on my pages when I need data from Session for user rights (stored in datatable) I look into session and retrieve DataTable:

User _currUser = (User)Session["User"];
DataTable dt = new DataTable();
dt = _currUser.UserRights;

This code works OK, but some strange behavior starts when I look into Session for DataTable UserRights multiple times on the same page.

Example: I have two user controls on page and in each one of them I look into session for DataTable like in the code above. Then if I change DataTable object instance in one of the user controls code like this for example:

dt.Columns.Remove("RoleID");

...this change impacts the instance in second user control too(??). And the same goes if give instances different names:

// FirstUserControl.ascx.cs
User _currUser1 = (User)Session["User"];
DataTable dt1 = new DataTable();
dt1 = _currUser1.UserRights;
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = new DataTable();
dt2 = _currUser2.UserRights; <--- dt2 is without column "RoleID" too!? 

Can somebody in a clear way explain to me why this is happening and how instances and references works in this example (it looks I don't understand them correctly). I always thought that if I execute code like the one above I would get dt1 and dt2 DataTable object which will not have anything in common when processing them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-12-06 09:46:26

DataTable 是引用类型,dt1 和 dt2 只是保存数据表的内存块的内存地址,它们始终引用同一个对象。

在这种情况下,您可以使用 DataTable.Copy()

User _currUser1 = (User)Session["User"];
DataTable dt1 = _currUser1.UserRights.Copy();
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = _currUser2.UserRights.Copy();

DataTable is a reference type, dt1 and dt2 are just memory addresses to the chunk of memory holding the datatable, they always refer to the same object.

In this case you can use DataTable.Copy()

User _currUser1 = (User)Session["User"];
DataTable dt1 = _currUser1.UserRights.Copy();
dt1.Columns.Remove("RoleID")

// SecondUserControl.ascx.cs
User _currUser2 = (User)Session["User"];
DataTable dt2 = _currUser2.UserRights.Copy();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文