views:

156

answers:

1

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
}
+3  A: 

First of all, you should inherit from System.Collections.ObjectModel.Collection<T>, not List<BusinessObject>. It's better to inherit from Collection<T> so that you can have control over all changes to the list (even changes made through IList<T>) by overriding InsertItem, SetItem, etc. It's probably also better to inherit from a collection of T than a collection of BusinessObject so that the methods and indexer 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'll need to serialize and deserialize the collection yourself; see here for instructions.

SLaks
Thank you, I am already doing this serializing for all my business objects, but I thought List<T> were serializable by default.And I am going to inherit from Collection<T> from now on. Thanks.
leandrosa81