Click here to Skip to main content
Rate this: bad
good
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
Seatech325

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 at 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
0 Sergey Alexandrovich Kryukov 646
1 OriginalGriff 385
2 CPallini 240
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,158
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2014
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