【发布时间】:2010-06-15 21:22:39
【问题描述】:
我有这个扩展方法来克隆我的 LINQ To SQL 对象:
public static T CloneObjectGraph<T>(this T obj) where T : class
{
var serializer = new DataContractSerializer(typeof(T), null, int.MaxValue, false, true, null);
using (var ms = new System.IO.MemoryStream())
{
serializer.WriteObject(ms, obj);
ms.Position = 0;
return (T)serializer.ReadObject(ms);
}
}
但是当我携带未加载所有引用的对象时,在使用 DataLoadOptions 查询时,有时它会抛出对象已处理的异常,但事情是我 不要要求未加载的引用(空)。
例如我有很多引用的客户,我只需要继续记忆地址引用 EntityRef 并且我不加载任何其他内容。但是,当我克隆对象时,这个异常迫使我使用 Customer 对象加载所有 EntitySet 引用,这可能太多并减慢应用程序速度。
有什么建议吗?
【问题讨论】:
标签: linq-to-sql serialization c#-3.0 clone datacontract