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

  • Home
  • SEARCH
  • 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 6168167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:38:22+00:00 2026-05-23T22:38:22+00:00

My application is a small C# database, and I’m using BinaryWriter to save the

  • 0

My application is a small C# database, and I’m using BinaryWriter to save the database to a file which is working fine with the basic types such as bool, uint32 etc.
Although I’ve got a variable that is of type Object (allowing the user to store any data type), however as my application doesn’t (nor needs to) know the real type of this variable, I’m unsure how to write it using BinaryWriter.
Is there a way I could perhaps grab the memory of the variable and save that? Would that be reliable?

Edit:

The answer provided by ba_friend has two functions for de/serialising an Object to a byte array, which can be written along with its length using a BinaryWriter.

  • 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-05-23T22:38:23+00:00Added an answer on May 23, 2026 at 10:38 pm

    You can use serialization for that, especially the BinaryFormatter to get a byte[].
    Note: The types you are serializing must be marked as Serializable with the [Serializable] attribute.

    public static byte[] SerializeToBytes<T>(T item)
    {
        var formatter = new BinaryFormatter();
        using (var stream = new MemoryStream())
        {
            formatter.Serialize(stream, item);
            stream.Seek(0, SeekOrigin.Begin);
            return stream.ToArray();
        }
    }
    

    public static object DeserializeFromBytes(byte[] bytes)
    {
        var formatter = new BinaryFormatter();
        using (var stream = new MemoryStream(bytes))
        {
            return formatter.Deserialize(stream);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a small business application which uses Sqlserver 2005 database. Platform: .Net
I'm designing a small SQL database to be used by a web application. Let's
I am creating a small application that will be deployed on Window. The database
A new client of mine has a small VB/Access database application written in 2002
I have a small application I am working on that at one point needs
I have a small application to convert several file formats, with a main windows
I am working on a small application in VB.NET. The program needs administrator privilege
I am here to write a small database application that will be running in
I'm currently in the research phase for a (very) small database application. It's for
I'm building a small web application with ASP.NET MVC 2, using db4o as 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.