我正在嘗試使用下面的代碼製作L2S實體的深層副本。我有.DBML的序列化模式設置爲單向。但是,當我嘗試使用下面顯示的DeepCopy方法制作L2S實體的深層副本時,出現錯誤,指出對象未標記爲可序列化。有人知道爲什麼Linq to Sql - 無法制作L2S實體的深層副本
public static T DeepCopy<T>(T obj)
{
object result = null;
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
result = (T)formatter.Deserialize(ms);
ms.Close();
}
return (T)result;
}
我的一個L2S類定義如下所示:
[global::System.Data.Linq.Mapping.TableAttribute(Name="Polar.Recipe")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class RecipeRecord : INotifyPropertyChanging, INotifyPropertyChanged
爲什麼你需要做一個L2S對象的深層副本? – Mig
只需使用DataContractSerializer。 – Marc