. NET trap a ideserializationcallback problems caused by

Source: Internet
Author: User

There is a class in the code that contains a dictionary (Dictionary<key, value>) that would have intended the former to implement the Ideserializationcallback interface in order to initialize the contents of the dictionary when deserializing. The result loop dictionary element code is not to go. It took a lot of effort to find out why, first look at the problematic code:

Using System;   
 Using System.Collections.Generic;   
 Using System.IO;   
 Using System.Runtime.Serialization;   
        
 Using System.Runtime.Serialization.Formatters.Binary;   
         namespace Dotnetbugs {[Serializable] public class Example:ideserializationcallback {   
        
         Private dictionary<string, string> map = new dictionary<string, string> (); Public Example () {map.   
             ADD ("One", "1"); Map.   
         ADD ("Two", "2");   
         public void OnDeserialization (object sender) {Dump ();   
                 public void Dump () {foreach (var item in map) { Console.WriteLine (item. Key + "->" + item.   
             Value); }} public class Starter {public static void Main (string[] Arg s) {   
             using (var stream = new MemoryStream ()) {var formatter = new Binaryf   
                 Ormatter (); Formatter.   
        
                 Serialize (Stream, New Example ()); Stream.   
                 Seek (0, seekorigin.begin); var example = (example) formatter.   
        
                 Deserialize (stream);   
                 Console.WriteLine ("After Deserialize"); Example.   
        
                 Dump ();   
             Console.read (); }   
         }   
     }   
 }

What kind of output do you expect the console to have?

One-> 1               |   
Two-> 2               | When the 44th row is deserialized, the output of the dump is called in example.ondeserialization. After   
deserialize one   
-> 1               |   
Two-> 2               | Call dump output on line 47th

But the actual output is:

After deserialize one   
-> 1   
two-> 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.