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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:33:52+00:00 2026-06-10T16:33:52+00:00

I have a class, that should support version tolerant serialization [Serializable] class A {

  • 0

I have a class, that should support version tolerant serialization

[Serializable]
class A {
    [OptionalField]
    int a;

    [OptionalField]
    MyClass b;

    [OptionalField]
    MyClass c;
}
  • How can I correct missing fields after deserialization? I guess, I have to use method marked with [OnDeserializing]. But how can I get which of fields was ignored?
  • Can I configure auto-deserialization to initialize field by default constructor in case of them missing?
  • 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-10T16:33:54+00:00Added an answer on June 10, 2026 at 4:33 pm

    Additionally, you can use OnSerializingAttribute and OnSerializedAttribute to set the fields. As the example shows, fields that have been already set will keep their value. Note, however, that this is only the case if the field is set during the OnSerializing event. Fields set during the OnSerialized event will override the serialized value.

    EDIT: In this case you can check in your method (decorated with OnSerialized) if the field equals to null and instantiate when necessary. If there is the possibility that this field is never be used and its creation can be deferred, think about hiding the field in question behind a property and instantiate it lazily.

    Models.cs:

    using System;
    using System.Runtime.Serialization;
    
    namespace SerializationExample
    {
        [Serializable]
        public class Model
        {
            public Model(){
                A = new SomeClass();
            }
    
            [OptionalField]
            public int value;
    
            [OptionalField]
            public SomeClass A;
    
            [OptionalField]
            public AnotherClass B;            
    
            [OnDeserializing]
            void OnDeserializing(StreamingContext context)
            {
                B = new AnotherClass("Set during deserializing");
            }
    
            [OnDeserialized]
            void OnDeserialized(StreamingContext context)
            {
                // Do sth. here after the object has been deserialized
            }
    
            public override string ToString()
            {
                return String.Format("A: {0}\nB: {1}", A, B);
            }
    
        }
    
        [Serializable]
        public class SomeClass
        {
            public string Value { get; set; }
    
            public SomeClass()
            {
                Value = "Default";
            }
    
            public override string ToString()
            {
                return Value;
            }
        }
    
        [Serializable]
        public class AnotherClass
        {
            public string Value { get; private set; }
    
            public AnotherClass(string v)
            {
                Value = v;
            }
    
            public override string ToString()
            {
                return Value;
            }
        }
    }
    

    Program.cs:

    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    namespace SerializationExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] FileNames = new string[] {
                    @"model1.bin",
                    @"model2.bin"
                };
    
                Stream[] files = new Stream[] {
                    File.Create(FileNames[0]),
                    File.Create(FileNames[1])
                };
    
                BinaryFormatter bf = new BinaryFormatter();
    
                Model m1 = new Model();
                m1.B = new AnotherClass("Set in app");
                m1.A.Value = "Set in app";
    
                Model m2 = new Model();
    
                Console.WriteLine("M1:\n{0}\n", m1);
                Console.WriteLine("M2:\n{0}\n\n", m2);
    
                bf.Serialize(files[0], m1);
                bf.Serialize(files[1], m2);
    
                foreach (var f in files)
                    f.Seek(0, SeekOrigin.Begin);
    
                m1 = null;
                m2 = null;
    
                m1 = (Model)bf.Deserialize(files[0]);
                m2 = (Model)bf.Deserialize(files[1]);
    
                Console.WriteLine("M1:\n{0}\n", m1);
                Console.WriteLine("M2:\n{0}\n\n", m2);
    
                foreach (var f in files)
                    f.Close();           
            }
        }
    }
    

    Output:

    M1:
    A: Set in app
    B: Set in app
    
    M2:
    A: Default
    B:
    
    
    M1:
    A: Set in app
    B: Set in app
    
    M2:
    A: Default
    B: Set during deserializing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a helper class that should only ever run in a background thread.
Scala programmer should have known that this sort of writing : class Person{ var
I have class A such that: class A { static int i; A(); f1();
This is quite hard to explain. I have a class which should support the
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different
I have class grades that I need to check if a specific grade is
I have a class that I instantiate to save or load xml data. For
I have a Class that has a private variable with a public setter/getter function:

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.