2009-03-23 15 views
21

निम्न उदाहरण को देखते हुए:deserialization के दौरान संरक्षक नहीं मिला?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 

namespace SerializationTest 
{ 
    [Serializable] 
    class Foo : Dictionary<int, string> 
    { 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      Foo foo = new Foo(); 
      foo[1] = "Left"; 
      foo[2] = "Right"; 

      BinaryFormatter formatter = new BinaryFormatter(); 
      MemoryStream stream = new MemoryStream(); 

      formatter.Serialize(stream, foo); 
      stream.Seek(0, SeekOrigin.Begin); 
      formatter.Deserialize(stream); 
     } 
    } 
} 

अंतिम पंक्ति में, क्योंकि फ़ॉर्मेटर फू करने के लिए निर्माता नहीं मिल सकता है एक SerializationException फेंक दिया है। ऐसा क्यों है?

उत्तर

49

वर्ग फू

public Foo() { 

} 

public Foo(SerializationInfo info, StreamingContext context) : base(info, context) { 

} 

वर्ग प्रासंगिक क्रमबद्धता मानकों के साथ एक निर्माता की जरूरत में निम्न कोड लाइनों जोड़ें।

+5

और सभी क्योंकि शब्दकोश ISERializable लागू करता है, निश्चित रूप से ... –

+0

SerializationInfo और StreamingContext कक्षाओं के लिए नोट, वे ** सिस्टम में हैं। रनटाइम। सरलीकरण ** नामस्थान। पूर्ण पथ ** System.Runtime.Serialization.SerializationInfo ** है। – Wappenull

संबंधित मुद्दे