Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's up with the [OptionalField] Attribute?
    primarykey
    data
    text
    <p>As I understand it I have to adorn a new member in a newer version of my class with the [OptionalField] Attribute when I deserialize an older version of my class that lacks this newer member.</p> <p>However, the code below throws no exception while the InnerTranslator property was added after serializing the class. I check for the property to be null in the onDeserialization method (which confirms that it was not serialized),but I would have expected the code to throw an exception because of that. Is the [OptionalField] Attribute itself optional?</p> <pre><code>class Program { static void Main(string[] args) { var listcol = new SortedList&lt;string,string&gt; { {"Estados Unidos", "United States"}, {"Canadá", "Canada"}, {"España", "Spain"} }; var translator = new CountryTranslator(listcol); using (var file_stream=new FileStream("translator.bin",FileMode.Open)) { var formatter = new BinaryFormatter(); translator = formatter.Deserialize(file_stream) as CountryTranslator; file_stream.Close(); } Console.ReadLine(); } } [Serializable] internal class CountryTranslator:IDeserializationCallback { public int Count { get; set; } public CountryTranslator(SortedList&lt;string,string&gt; sorted_list) { this.country_list = sorted_list; inner_translator = new List&lt;string&gt; {"one", "two"}; } //[OptionalField] private List&lt;string&gt; inner_translator; public List&lt;string&gt; InnerTranslator { get { return inner_translator; } set { inner_translator = value; } } private SortedList&lt;string, string&gt; country_list; public void OnDeserialization(object sender) { Debug.Assert(inner_translator == null); Count=country_list.Count; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload