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 8220047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:23:10+00:00 2026-06-07T13:23:10+00:00

I am trying to add a save method to a List that I can

  • 0

I am trying to add a save method to a List that I can call and serialize the object to a file. I’ve got everything figured out except how to get the base class itself.

Here’s my code:

/// <summary>
/// Inherits the List class and adds a save method that writes the list to a stream.
/// </summary>
/// <typeparam name="T"></typeparam>
class fileList<T> : List<T>
{
    private static IFormatter serial = new BinaryFormatter();
    private Stream dataStream;

    /// <summary>
    /// path of the data file.
    /// </summary>
    public string dataFile { get; set; }
    /// <summary>
    /// Sets the datafile path
    /// </summary>
    public fileList(string dataFile)
    {
        this.dataFile = dataFile;
    }
    /// <summary>
    /// Saves the list to the filestream.
    /// </summary>
    public void Save()
    {
        dataStream = new FileStream(dataFile,
            FileMode.Truncate, FileAccess.Write,
            FileShare.Read);
        //Right here is my problem. How do I access the base class instance.
        serial.Serialize(dataStream, this.base); 
        dataStream.Flush();
        dataStream.Close();
        dataStream = null;
    }
}
  • 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-07T13:23:11+00:00Added an answer on June 7, 2026 at 1:23 pm

    The line

    serial.Serialize(dataStream, this.base); 
    

    should just be

    serial.Serialize(dataStream, this); 
    

    Note however (thanks @Anders) that this will also serialize string dataFile. To avoid that, decorate that property with NonSerializedAttribute.

    Having said that, I prefer to implement this type of functionality as a static method. With the advent of extension methods, I created a small extension class to handle this for any serializable type:

    static public class SerialHelperExtensions
    {
        static public void Serialize<T>(this T obj, string path)
        {
            SerializationHelper.Serialize<T>(obj, path);
        }
    }
    
    static public class SerializationHelper
    {
        static public void Serialize<T>(T obj, string path)
        {
    
            DataContractSerializer s = new DataContractSerializer(typeof(T));
            using (FileStream fs = File.Open(path, FileMode.Create))
            {
                s.WriteObject(fs, obj);
            }
        }
    
        static public T Deserialize<T>(string path)
        {
            DataContractSerializer s = new DataContractSerializer(typeof(T));
            using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
            {
                object s2 = s.ReadObject(fs);
                return (T)s2;
            }
        }
    }
    

    You can certainly substitute BinaryFormatter for DataContractSerializer and use the same pattern.

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

Sidebar

Related Questions

Trying to add init parameter names to a list in init(ServletConfig) method. public void
Trying to add a class object into a List using reflection, but when invoking
I have a method that converts a file to bytes so that I can
Im new to asp.net mvc. I'm trying add new model class but it got
I am trying to apply a save method in a backing Java bean which
I am trying to save a modelform that represents a bank account but I
I am trying to save a list of over 10000 records with CakePHP 2.1's
I am trying to call a jQuery action within a div (subpage) that was
Im trying to write an object i made onto a file using Object output
I'm trying to have a View where the user can add items in a

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.