Serialized specialized list <>

I have a specialized BusinessObjectList class with this declaration:

public class BusinessObjectList<T> : List<BusinessObject> where T: BusinessObject {}

      

And I want to serialize objects of this class, for example:

info.AddValue("myList", myList);

      

I tried adding the ISerializable interface, 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 subclassing of them?

EDIT:

For future reference only, here is the entire class:

BusinessObjectList.cs (When you call save (), it automatically saves all inserts to the DB and removes from the list, and also saves all changed values ​​in 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
}

      

+2


source share


1 answer


First of all, you must inherit from System.Collections.ObjectModel.Collection<T>

, not List<BusinessObject>

. It's better to inherit from Collection<T>

so that you can control all changes in the list (even changes made with IList<T>

) by overriding InsertItem, SetItem, etc. It's probably also better to inherit from a collection T

than a set BusinessObject

so that the methods and indexer that you inherit from your base class use the correct type.



To answer your question, none of the .Net collection types (including Collection<T>

) are serializable. You will need to serialize and deserialize the collection yourself; see here for instructions.

+4


source







All Articles