我有一個專門的類,BusinessObjectList,同意本聲明:序列化專業名單<>
public class BusinessObjectList<T> : List<BusinessObject> where T: BusinessObject {}
我想序列化這個類的對象,如:
info.AddValue("myList", myList);
我試圖添加界面ISerializable,但沒有成功。
public class BusinessObjectList<T> : List<BusinessObject>, ISerializable where T: BusinessObject {}
反序列化時該列表總是空的。 我覺得列表<>對象是可序列化的,但是它們的子類呢?
編輯:
只是以供將來參考,這裏是整個類:
BusinessObjectList.cs(當你調用保存(),它會自動持續在數據庫中的所有刀片,並從列表中刪除,並也節省了對業務對象的所有修改後的值):
[Serializable()]
public class BusinessObjectList<T> : List<BusinessObject>,ISerializable where T: BusinessObject
{
public delegate void BusinessObjectListDeleteHandler(T objDeleted);
public event BusinessObjectListDeleteHandler deleteHandler;
List<BusinessObject> objsForDelete = new List<BusinessObject>();
public BusinessObjectList()
{
}
public new void Add(BusinessObject item)
{
base.Add(item);
}
public new T this[int index]
{
get
{
return (T)base[index];
}
}
public bool save()
{
foreach (T obj in objsForDelete)
{
if (deleteHandler != null)
deleteHandler(obj);
obj.delete();
}
objsForDelete.Clear();
foreach (BusinessObject obj in this)
{
obj.save();
}
return true;
}
public new void Clear()
{
foreach (BusinessObject obj in this)
{
//obj.delete();
objsForDelete.Add(obj);
}
base.Clear();
}
public new void Remove(BusinessObject obj)
{
// obj.delete();
objsForDelete.Add(obj);
base.Remove(obj);
}
public new void RemoveAt(int index)
{
objsForDelete.Add(this[index]);
base.RemoveAt(index);
}
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// should I do something here??
}
#endregion
}
而且BusinessObject.cs:
[Serializable()]
public abstract class BusinessObject:ISerializable
{
public abstract bool save();
public abstract bool delete();
public abstract DbTransaction startTransaction();
public abstract void useTransaction(DbTransaction transaction);
public abstract DbTransaction getTransaction();
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// nothing to serialize here
}
#endregion
}
就我個人而言,我認爲有很多很好的理由,不要在這裏使用'BinaryFormatter';特別是如果您打算保存數據 - 基於合同的方法(如「XmlSerializer」)將更加穩定。 – 2009-10-01 22:05:55
我需要序列化它,因爲我在IIS上的會話存儲在SQL Server上,而不是in-proc。是否有可能爲此目的使用XMLSerializer? – leandrosa81 2009-10-02 12:48:42