現在就完成它。我用[Serializable]
標記了包含Linqobject的對象,幷包含了ISerializable
接口。 DBOItem
是我LinqObject
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
PropertyInfo[] infos = this.DBOItem.GetType().GetProperties();
foreach (PropertyInfo pi in infos)
{
bool isAssociation = false;
foreach (object obj in pi.GetCustomAttributes(true))
{
if (obj.GetType() == typeof(System.Data.Linq.Mapping.AssociationAttribute))
{ isAssociation = true; break; }
}
if (!isAssociation)
{
if (pi.GetValue(this.DBOItem, null) != null)
{
info.AddValue(pi.Name, pi.GetValue(this.DBOItem, null));
}
}
}
}
的構造函數需要被列入deserialisation看起來是這樣的:
protected BusinessObjectBase(SerializationInfo info, StreamingContext context)
{
this.DBOItem = new T();
PropertyInfo[] properties = this.DBOItem.GetType().GetProperties();
SerializationInfoEnumerator enumerator = info.GetEnumerator();
while (enumerator.MoveNext())
{
SerializationEntry se = enumerator.Current;
foreach (PropertyInfo pi in properties)
{
if (pi.Name == se.Name)
{
pi.SetValue(this.DBOItem, info.GetValue(se.Name, pi.PropertyType), null);
}
}
}
}