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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:17:32+00:00 2026-05-27T00:17:32+00:00

I have an application that serializes data using BinaryFormatter . A member was added

  • 0

I have an application that serializes data using BinaryFormatter. A member was added to the class that was serialized from one version to the next without changing the class name. Code was added to handle the possible absence of the added member in old serialized files:

private void readData(FileStream fs, SymmetricAlgorithm dataKey)
{
    CryptoStream cs = null;

    try
    {
        cs = new CryptoStream(fs, dataKey.CreateDecryptor(),
            CryptoStreamMode.Read);
        BinaryFormatter bf = new BinaryFormatter();

        string string1 = (string)bf.Deserialize(cs);
        // do stuff with string1

        bool bool1 = (bool)bf.Deserialize(cs);
        // do stuff with bool1

        ushort ushort1 = (ushort)bf.Deserialize(cs);
        // do stuff with ushort1

        // etc. etc. ...

        // this field was added later, so it may not be present
        // in the serialized binary data.  Check for it, and if
        // it's not there, do some default behavior

        NewStuffIncludedRecently newStuff = null;

        try
        {
            newStuff = (NewStuffIncludedRecently)bf.Deserialize(cs);
        }
        catch
        {
            newStuff = null;
        }

        _newStuff = newStuff != null ?
                new NewStuffIncludedRecently(newStuff) :
                new NewStuffIncludedRecently();
    }
    catch (Exception e)
    {
        // ...
    }
    finally
    {
        // ...
    }
}

The point I’m at now is that I’d really like to just rinse and repeat with another member I’d like to add, which would mean I’d add another field and try-catch block similar to that for NewStuffIncludedRecently.

I had thought of just making the entire class [Serializable] but wouldn’t that break compatibility with the old serialized data?

My main concern is that I’m not clear how the deserialization works. If I add in handling for another optional field similarly to above, will it work? What are other options I have for handling these changes better?

Thanks in advance as always.

  • 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-27T00:17:32+00:00Added an answer on May 27, 2026 at 12:17 am

    If you mark the new fields with [OptionalField] it should work, but I have heard reports of flakiness in some cases. I can’t say for sure, since I avoid BinaryFormatter, because it has so many issues when versioning 🙂 (plus, it isn’t as “tight” as some alternatives, and has severe issues if you want to go cross-platform, or to CF/SL etc)

    If you are implementing ISerializable, you might try:

    foreach(SerializationEntry entry in info) {
        switch(entry.Name) {
             case "Name": Name = (string)info.Value;
             case "Id": Id = (int)info.Value;
             ...
        }
    }
    

    But again, must stress – this is doing things the hard way :p
    With this approach, you only process the data that is actually there.

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

Sidebar

Related Questions

Currently i have an application that reads and writes several properties from one or
We have an application that takes user data in C#, serializes it and puts
I have an application that I'm converting to Symfony/Doctrine. It currently stores a serialized
I have an application that reads a CSV file with piles of data rows.
I have a winform application that uses some referenced web services to get data.
I am using asp.net MVC to develop an application that will have ajax interactions.
I have an application that reads records from the database and exports it out
I have an application I've written in PHP from scratch. I'm using PHP's native
I have a .NET Windows Mobile 5.0 application that is used for data collection.
We have an application that works with financial data that is schemaless. More accurately,

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.