Click here to Skip to main content
14,935,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How should I serialize a list of actions (ie. List<iAction> toDo;) for saving to a file or sending over a LAN? I was going to use XmlSerializer, but I changed my List to include more than one type of action and now my plan doesn't work anymore.

Should I add a method to iAction such as iAction.Serialize and serialize each action instead of the whole list, or do I need to come up with something else? Btw, I'm using XmlSerializer because I want to save the list to an XML file for other reasons.

Here is what I was doing (simplified):
enum Shape { line, circle, square }

class Action
{
    Point start;
    Point stop;
    Shape drawShape;

    void Draw(Graphics gfx)
    {
        switch(drawShape)
            // gfx.DrawLine / gfx.DrawElipse / gfx.DrawRectangle
    }
}

void Main()
{
    List<Action> toDo;
    toDo.Add(Action);
    toDo.Add(Action);
    toDo.Add(Action);

    XmlSerializer.Serialize(saveFile, toDo);
}


Now I have an action interface and different actions inheriting from it:
interface iAction
{
    void Start(Point start);
    void onMove(Point movedTo);
    void Stop(Point stop);
    void Draw(Graphics gfx);
}

class Line : iAction { ... }
class Path : iAction { ... }
class Square : iAction { ... }
...

void Main()
{
    List<iAction> toDo;
    toDo.Add(Line);
    toDo.Add(Circle);
    toDo.Add(Square);

    XmlSerializer.Serialize(saveFile, toDo); // "Error: Can not serialize interface iAction."

}
Posted
Updated 16-Feb-10 12:12pm
v2

1 solution

You need to use the SerializableAttribute and other hints. Have a look here[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900