我正在尝试这样做:
Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
GlobalHelper.GetCurrentSession()["Data4JS"] = null;
return D4JS;
问题是它也将 D4JS 设置为 null,我真的不想将我的代码分成几个方法调用,我还能如何轻松地实现这一点?
我正在尝试这样做:
Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
GlobalHelper.GetCurrentSession()["Data4JS"] = null;
return D4JS;
问题是它也将 D4JS 设置为 null,我真的不想将我的代码分成几个方法调用,我还能如何轻松地实现这一点?
只需使用 new 关键字,这样您就不会使用引用或克隆对象方法。
在这种情况下,使用序列化进行深层复制。
假设您的对象足够简单并且具有公共属性,并且您需要的只是属性中的值,您可以执行类似的操作:
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 的建议作为实现您需要的更清洁的方法。