Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8450539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:59:42+00:00 2026-06-10T10:59:42+00:00

I have a class. I wanna serialize and the serialize a list that contains

  • 0

I have a class. I wanna serialize and the serialize a list that contains object of that class. I want to use binary formatter.
This is my class:

[Serializable]
public class Section :ISerializable
{
    private double ClearCover;
    private string SectionName;

    private double Diameter;

    public double Height;
    private double Width;

    private double InternalDiameter;
    private double ExternalDiameter;

    private double InternalHeight;
    private double ExternalHeight;
    private double InternalWidth;
    private double ExternalWidth;

    private double WebWidth;
    private double FlangeWidth;
    private double FlangeThickness;



    public double clearCover
    {
        get { return ClearCover; } 
        set { ClearCover = value; }
    }
    public string sectionName
    {
        get{return SectionName;} 
        set{SectionName=value;} 
    }

    public double diameter
    {
        get { return Diameter; } 
        set { Diameter = value; }
    }

    public double height
    {
        set { Height = value; }
        get { return Height; }
    }
    public double width
    {
        set { Width = value; }
        get { return Width; }
    }

    public double internalDiameter
    {
        set { InternalDiameter = value; } 
        get { return InternalDiameter; }
    }
    public double externalDiameter
    {
        set { ExternalDiameter = value; } 
        get { return ExternalDiameter; }
    }

    public double internalHeight
    {
        set { InternalHeight = value; }
        get { return InternalHeight; }
    }
    public double externalHeight
    {
        set { ExternalHeight = value; } 
        get { return ExternalHeight; }
    }
    public double internalWidth
    {
        set { InternalWidth = value; } 
        get { return InternalWidth; }
    }
    public double externalWidth
    {
        set { ExternalWidth = value; } 
        get { return ExternalWidth; }
    }

    public double flangeWidth
    {
        set { FlangeWidth = value; }
        get { return FlangeWidth; }
    }
    public double flangeThickness
    {
        set { FlangeThickness = value; }
        get { return FlangeThickness; }
    }
    public double webWidth
    {
        set { WebWidth = value; }
        get { return WebWidth; }
    }




    public Section() { }

    protected Section(SerializationInfo info, StreamingContext context)
    {
        sectionName = info.GetString("Section Name");
        clearCover = info.GetDouble("Clear Cover");

        diameter = info.GetDouble("Diameter");

        //internalDiameter = info.GetDouble("Internal Diameter");
        //externalDiameter = info.GetDouble("External Diameter");


        height = info.GetDouble("Height");
        width = info.GetDouble("Width");



        internalHeight = info.GetDouble("Internal Height");
        externalHeight = info.GetDouble("External Height");
        internalWidth = info.GetDouble("Internal Width");
        externalWidth = info.GetDouble("External Width");

        flangeWidth = info.GetDouble("Flange Width");
        flangeThickness = info.GetDouble("Flange Thickness");
        webWidth = info.GetDouble("Web Width");



    }

    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Section Name", sectionName);
        info.AddValue("Clear Cover", clearCover);

        info.AddValue("Diameter",diameter);

        //info.AddValue("Internal Diameter", internalDiameter);
        //info.AddValue("External Diameter", externalDiameter);

        info.AddValue("Height",height);
        info.AddValue("Width",width);

        info.AddValue("Internal Height",internalHeight);
        info.AddValue("External Height",externalHeight);
        info.AddValue("Internal Width",internalWidth);
        info.AddValue("External Width",externalWidth);

        info.AddValue("Flange Width",flangeWidth);
        info.AddValue("Flange Thickness", flangeThickness);
        info.AddValue("Web Width", webWidth);


    }


}

I don’t have any problem in serialization. But for deserialize, it can just deserialize first object not all of them. What should I do, please help me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:59:43+00:00Added an answer on June 10, 2026 at 10:59 am

    From a separate email from the OP, the following methods were obtained (I’ve tidied them a little, not much):

        static void Serialize(object obj, string filename)
        {
            using (FileStream streamOut = new FileStream(filename, FileMode.Append))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(streamOut, obj);
            }
        }
    
    
        public static List<Section> DeserializeList(string filename)
        {
            using (FileStream streamIn = File.OpenRead(filename))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return (List<Section>) formatter.Deserialize(streamIn);
            }
        }
    

    The key observation here is that it is assuming that BinaryFormatter is an appendable format, and that serializing two objects sequentially is the same as serializing 2 items at the same time (a list), for example:

        if(File.Exists("foo.bin")) File.Delete("foo.bin"); // start afresh
        Serialize(new Section { Diameter = 1.2, ClearCover = 3.4 }, "foo.bin");
        Serialize(new Section { Diameter = 5.6, ClearCover = 7.8 }, "foo.bin");
    
        var clone = DeserializeList("foo.bin");
    

    However, that simply is not the case. To deserialize the items in this way, we would have to do something like:

        public static List<Section> DeserializeList(string filename)
        {
            using (FileStream streamIn = File.OpenRead(filename))
            {
                List<Section> list = new List<Section>();
                BinaryFormatter formatter = new BinaryFormatter();
                while(streamIn.Position != streamIn.Length)
                {
                    list.Add((Section) formatter.Deserialize(streamIn));
                }
                return list;
            }
        }
    

    which it a little vexing – not least because it can’t be applied to all streams (.Length and even .Position are not universally available).

    The above should work, but I would strongly advise using an appendable format, such as protobuf. In fact, protobuf-net makes this scenario effortless, both in terms of the model:

        [ProtoContract]
        public class Section
        {
            [ProtoMember(1)] public double ClearCover { get; set; }
            [ProtoMember(2)] public string SectionName { get; set; }
            [ProtoMember(3)] public double Diameter { get; set; }
            [ProtoMember(4)] public double Height { get; set; }
            [ProtoMember(5)] public double Width { get; set; }
            [ProtoMember(6)] public double InternalDiameter { get; set; }
            [ProtoMember(7)] public double ExternalDiameter { get; set; }
            [ProtoMember(8)] public double InternalHeight { get; set; }
            [ProtoMember(9)] public double ExternalHeight { get; set; }
            [ProtoMember(10)] public double InternalWidth { get; set; }
            [ProtoMember(11)] public double ExternalWidth { get; set; }
            [ProtoMember(12)] public double FlangeWidth { get; set; }
            [ProtoMember(13)] public double FlangeThickness { get; set; }
            [ProtoMember(14)] public double WebWidth { get; set; }
        }
    

    and the serialization / deserialization:

        static void Main()
        {
            if (File.Exists("foo.bin")) File.Delete("foo.bin"); // start afresh
    
            Serialize(new Section { Diameter = 1.2, ClearCover = 3.4 }, "foo.bin");
            Serialize(new Section { Diameter = 5.6, ClearCover = 7.8 }, "foo.bin");
    
            var clone = DeserializeList("foo.bin");
        }
        static void Serialize(object obj, string filename)
        {
            using (FileStream streamOut = new FileStream(filename, FileMode.Append))
            {
                Serializer.NonGeneric.SerializeWithLengthPrefix(
                    streamOut, obj, PrefixStyle.Base128, Serializer.ListItemTag);
            }
        }
    
    
        public static List<Section> DeserializeList(string filename)
        {
            using (FileStream streamIn = File.OpenRead(filename))
            {
                return Serializer.DeserializeItems<Section>(
                    streamIn, PrefixStyle.Base128, Serializer.ListItemTag).ToList();
            }
        }
    

    Finally, the BinaryFormatter data for those 2 rows was 750 bytes; with protobuf-net: 40 bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna serialize this class: [Serializable] [XmlRoot(ElementName = Rates)] public class CbrRate : IRate
I have a Class CButtonCreate without get & set Methode. But i wanna use
I have class like this below shown. which contains the shopping items where the
I have a dll that has a class called Series. This class has a
I have a class like this: public class A { public static void main()
I have the following: public Class Vacancy{ public int VacancyID {get;set;} public List<Application> Applications
I have a class that implements Serializable , it's part of a bigger mesh
I have this code: public class Configuration{ public Control container; public Configuration() { container
Suppose I have a class and I want to use it somewhere as a
I have this form: class TourForm(ModelForm): class Meta: model = Tour I wanna design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.