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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:40:01+00:00 2026-05-23T02:40:01+00:00

I need to make all my entities serializable. So I was thinking in a

  • 0

I need to make all my entities serializable. So I was thinking in a BaseEntity with a Backup and a Restore method. But in the restore I can’t override the object with the saved one because this is read-only.

Any solution or some other way to get the serializable entities?

My code:

internal class BaseEntity
{
    private MemoryStream ms = new MemoryStream();
    private BinaryFormatter bf = new BinaryFormatter();

    public void Backup()
    {
        bf.Serialize(ms, this);
    }

    public void Restore()
    {
        this = (BaseEntity)bf.Deserialize(ms);
    }
}
  • 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-23T02:40:02+00:00Added an answer on May 23, 2026 at 2:40 am

    Edit: One way serialization can work is to use the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter (or other implementation of IFormatter). To serialize an object you pass the object and a stream. To Deserialize the object, you pass a stream (positioned at the begining of your serialized data), and it returns the serialized object and all its depenedencies.

    public static class EntityBackupServices
    {
       public static MemoryStream Backup (BaseEntity entity)
       {
          var ms = new MemoryStream();
          Serialize (ms, entity);
          ms.Position = 0;
          return ms;
       }
       public static void Serialize (Stream stream, BaseEntity entity)
       {
          var binaryFormatter = new BinaryFormatter();
          binaryFormatter.Serialize (stream, entity);
       }
       public static BaseEntity Restore (Stream stream)
       {
          var binaryFormatter = new BinaryFormatter();
          var entity = (BaseEntity) binaryFormatter.Deserialize (stream);
          return entity;
       }
    }
    

    One thing a formatter don’t do (though the FormatterServices class makes it possible) is modify existing objects. So you probably don’t want to have an instance method called Deserialize. You can’t really do this: new LionEntity().Deserialize () where it replaces the fields of an existing instance.

    Note: You’ll need to put Serializable over all your types. Any fields that can’t be serialized (because it’s either not a struct, or it’s not marked as [Serializable] will need to be marked with NonSerialized.

    // A test object that needs to be serialized.
    [Serializable()]        
    public class BaseEntity
    {
        public int member1;
        public string member2;
        public string member3;
        public double member4;
    
        // A field that is not serialized.
        [NonSerialized()] public MyRuntimeType memberThatIsNotSerializable; 
    
        public TestSimpleObject()
        {
            member1 = 11;
            member2 = "hello";
            member3 = "hello";
            member4 = 3.14159265;
            memberThatIsNotSerializable = new Form ();
        }
    
        public MemoryStream Backup ()
        {
           return EntityBackupServices.Backup (this);
        }
    }
    

    Edit:
    The way I’ve mentioned is a rather standard and accepted way. If you want to venture into hackdom, you can deserialize the object the way I’ve mentioned, then use reflection to set each field on your existing object to the value of the deserialized object.

    public class BaseEntity
    {
       void Restore(Stream stream)
       {
          object deserialized = EntityBackupServices.RestoreDeserialize(stream);//As listed above
          if (deserialized.GetType () != this.GetType ())
             throw new Exception();
          foreach (FieldInfo fi in GetType().GetFields())
          {
             fi.SetValue(this, fi.GetValue (deserialized));
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a database that is highly localized. For almost all entities
I need a SQL query to make all data in a column UPPER CASE?
I found all the code I need to make SHBrowseForFolder work in my application.
I need to make a piece of C# code interact through COM with all
We need to make changes to an app that will cause all its URLS
All I need is a way to make a property of one class only
I have a crystal report file I need make a tiny edit in. It
I need to make a WebCast presentation soon and need to do some whiteboarding
I need to make an application in .NET CF with different/single forms with a
I need to make an ArrayList of ArrayLists thread safe. I also cannot have

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.