Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Very short question:

Why does this class(TCtsPageAdmin) have to be marked [Serializable]?
I'm just trying to serialize a
C#
List<TCtsPage>
TCtsPage class is marked properly.

If I mark TCtsPageAdmin properly with the [Serializable] attribute, then the error moves up the hierarchy -> the object that consumes a TCtsPageadmin object will be the target of the error.

Error:
SaveToFile failed:
Type 'Lrj.Cts.Admin.TCtsPageAdmin' in Assembly 'LrjCtsAdmin, Version=1.0.1.2,
Culture=neutral, PublicKeyToken=null' is not marked as serializable.


C#
[CLSCompliant(true)]
public sealed class TCtsPageAdmin : ILrjObj
{
 ...
    public void SaveToFile(bool aUsePagePath = true)
    {
        String aFileName;
        List<TCtsPage> lst = GetPages("*");

        aFileName = aUsePagePath ? Path.Combine(PagePath, "pages.dat") : 
                                                                    "pages.dat";

        BinaryFormatter binForm = new BinaryFormatter();
        try
        {
            using (Stream fStream = new FileStream(aFileName, FileMode.Create, 
                                              FileAccess.Write, FileShare.None))
            {
                binForm.Serialize(fStream, lst);
                OnSavedToFile(
                  new InfoEventArgs(
                      String.Format("SaveToFile succeeded.\n{0}", aFileName)));
            }
        }
        catch (Exception ex)
        {
            OnSaveToFileFailed(
              new InfoEventArgs(
                    String.Format("SaveToFile failed: \n{0}", ex.Message)));
        }
    }
 ...
}
Posted

1 solution

All object used in TCtsPage must have a [Serializable] attribute.

Alternatively if you can use my serializers which don't require anything and are faster than the BinaryFormatter:
fastJSON[^]

fastBinaryJSON[^]
 
Share this answer
 
Comments
Seatech 16-Aug-12 3:39am    
Thank you very much for the reply.
All objects in TCtsPage are marked with the [Serializable] attribute.
Your code looks very interesting. I think I'll have a look at that.

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