Click here to Skip to main content
12,453,183 members (61,740 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# serialization
Very short question:

Why does this class(TCtsPageAdmin) have to be marked [Serializable]?
I'm just trying to serialize a
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.


[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 15-Aug-12 20:41pm
Seatech385

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100