我有一个专门的类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 {}未序列化时,列表始终为null。我认为List 对象是可序列化的,但是它们的子类又如何呢?编辑:仅供以后参考,以下是整个课程:BusinessObjectList.cs(当您调用save()时,它将自动在数据库上持久存储所有插入和从列表中删除的操作,还将所有修改后的值保存在业务对象上):[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} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 首先,您应该继承System.Collections.ObjectModel.Collection<T>,而不是List<BusinessObject>。最好从Collection<T>继承,以便您可以通过覆盖InsertItem,SetItem等来控制列表的所有更改(甚至是通过IList<T>进行的更改)。从T的集合中继承也可能更好。而不是BusinessObject的集合,以便您从基类继承的方法和索引器使用正确的类型。要回答您的问题,.Net集合类型(包括Collection<T>)均不可序列化。您需要自己对集合进行序列化和反序列化;有关说明,请参见here。关于c# - 序列化专业列表<>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1506734/ (adsbygoogle = window.adsbygoogle || []).push({});
10-09 05:04