1

我正在尝试这样做:

   Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
    GlobalHelper.GetCurrentSession()["Data4JS"] = null; 
    return D4JS;

问题是它也将 D4JS 设置为 null,我真的不想将我的代码分成几个方法调用,我还能如何轻松地实现这一点?

4

3 回答 3

2

只需使用 new 关键字,这样您就不会使用引用或克隆对象方法。

深度克隆对象

于 2012-08-14T09:06:59.980 回答
0

在这种情况下,使用序列化进行深层复制。

于 2012-08-14T09:08:54.857 回答
0

假设您的对象足够简单并且具有公共属性,并且您需要的只是属性中的值,您可以执行类似的操作:

Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
Data4JS result = new Data4JS
{
    //Set your result object to the one in 
    //session by manually copying over the property values
    Property1 = D4JS.Property1;
};

GlobalHelper.GetCurrentSession()["Data4JS"] = null; 

return result;

否则,请使用 Freeman 的建议作为实现您需要的更清洁的方法。

于 2012-08-14T09:40:33.760 回答