Hi everyone,
I have a specialized class, BusinessObjectList, with this declaration:
public class BusinessObjectList<T> : List<BusinessObject> where T: BusinessObject {}
And I want to serialize objects of this class, like:
info.AddValue("myList", myList);
I tried to add the interface ISerializable, but with no success.
public class BusinessObjectList<T> : List<BusinessObject>, ISerializable where T: BusinessObject {}
The list is always null when unserialized. I think List<> objects are serializable, but what about subclasses of them?
EDIT:
Just for future reference, here is the entire class:
BusinessObjectList.cs (When you call save(), it automatically persists on DB all inserts and removes from the list, and also saves all modified values on the business objects):
[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
}
And 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
}