深度复制只有在进入方法时才能正常工作
Deep copy only works properly when stepping into method
我已经实现了一个深层复制方法,当进入这个方法时 - class 被按预期复制。
但是,如果我跳过该方法并检查 class 实例,则某些属性未正确复制。
Profile profile = new Profile();
profile = Model.Copy<Profile>(Profile.GetProfileById(ProfileID));
profile.Save();
上面的代码显示了 class 实例创建和调用副本的位置。
与数据库一致,我要求复制的配置文件在AddressCollection
属性.
中有2项
如果我在行 profile.Save()
上放置一个断点并跨过复制方法,则实例复制不正确并且 AddressCollection
属性 有 0 个项目。
但是,如果我实际进入复制方法,实例会被正确复制并且 returns AddressCollection
有 2 个项目。
复制方法
public T Copy<T>(T oldObject) where T : CRMBusinessObjectBase
{
return Copy<T>(oldObject, null, null);
}
public T Copy<T>(T oldObject, CRMBusinessObjectBase parentObject, string parentProperty) where T : CRMBusinessObjectBase
{
//Create copy of business object
T copy = null;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sz = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//Serialize and then deserialize to a new object
using (MemoryStream ms = new MemoryStream())
{
sz.Serialize(ms, oldObject);
ms.Position = 0;
copy = (T)sz.Deserialize(ms);
}
//Set business object as new
copy.SetAsNew();
//Set parent property if specified
if (parentObject != null && !string.IsNullOrEmpty(parentProperty))
{
copy.GetType().GetProperty(parentProperty).SetValue(copy, parentObject, null);
}
return copy;
}
我曾尝试将 BinaryFormatter
更改为使用 DataContractSerializer
,但没有成功。
我也尝试过使用反射而不是序列化,但出现了同样的问题。
不太确定问题出在哪里。
截图
复制前
复制后
此问题可能与使用 Watch window 或在调试时将鼠标悬停在变量上有关。
见http://dotdotnet.blogspot.com.au/2010/04/lazy-load-eager-debugging.html:
Another thing is that debugger and specifically Visual Studio Watch
window is not so friendly to lazy loading because the Watch window try
to evaluate the value of each property which cause all the lazy
loading properties to be loaded.
我已经实现了一个深层复制方法,当进入这个方法时 - class 被按预期复制。
但是,如果我跳过该方法并检查 class 实例,则某些属性未正确复制。
Profile profile = new Profile();
profile = Model.Copy<Profile>(Profile.GetProfileById(ProfileID));
profile.Save();
上面的代码显示了 class 实例创建和调用副本的位置。
与数据库一致,我要求复制的配置文件在AddressCollection
属性.
如果我在行 profile.Save()
上放置一个断点并跨过复制方法,则实例复制不正确并且 AddressCollection
属性 有 0 个项目。
但是,如果我实际进入复制方法,实例会被正确复制并且 returns AddressCollection
有 2 个项目。
复制方法
public T Copy<T>(T oldObject) where T : CRMBusinessObjectBase
{
return Copy<T>(oldObject, null, null);
}
public T Copy<T>(T oldObject, CRMBusinessObjectBase parentObject, string parentProperty) where T : CRMBusinessObjectBase
{
//Create copy of business object
T copy = null;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter sz = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//Serialize and then deserialize to a new object
using (MemoryStream ms = new MemoryStream())
{
sz.Serialize(ms, oldObject);
ms.Position = 0;
copy = (T)sz.Deserialize(ms);
}
//Set business object as new
copy.SetAsNew();
//Set parent property if specified
if (parentObject != null && !string.IsNullOrEmpty(parentProperty))
{
copy.GetType().GetProperty(parentProperty).SetValue(copy, parentObject, null);
}
return copy;
}
我曾尝试将 BinaryFormatter
更改为使用 DataContractSerializer
,但没有成功。
我也尝试过使用反射而不是序列化,但出现了同样的问题。
不太确定问题出在哪里。
截图
复制前
此问题可能与使用 Watch window 或在调试时将鼠标悬停在变量上有关。
见http://dotdotnet.blogspot.com.au/2010/04/lazy-load-eager-debugging.html:
Another thing is that debugger and specifically Visual Studio Watch window is not so friendly to lazy loading because the Watch window try to evaluate the value of each property which cause all the lazy loading properties to be loaded.