I tried to undertand the ISerializable and stumped on this. I made two classes both with the attribute "Serializable". Only one class is derived from ISerializable and GetObjectData was defined for it. Let us call this class A. Another was not derived from ISerializable there by GetObjectData was not defined for that. Let us call this class as B. I didnt provide any special constructor for class A. Now in the run time class A is showing the error like "Special constructor is missing". The syntax is same for both the classes. So, the error may be some other thing but it shouldn't be regarding the constructor. Otherwise I should get the same error for class B too. See the code below. Can anybody say the reason behind it?
Note: I am using windows 7 - 64 bit with visual studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Delete_This
{
[Serializable]
class After_Implementing_ISerializable:ISerializable
{
int a;
string b;
public After_Implementing_ISerializable(int a, string b)
{
this.a = a;
this.b = b;
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
public void Check()
{
After_Implementing_ISerializable s = new After_Implementing_ISerializable(15, "100");
FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, s);
fs.Close();
fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
After_Implementing_ISerializable d = (After_Implementing_ISerializable)bf.Deserialize(fs);
fs.Close();
}
}
[Serializable]
class Without_Implementing_ISerializable
{
int a;
string b;
public Without_Implementing_ISerializable(int a,string b)
{
this.a = a;
this.b = b;
}
public void Check()
{
Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(15, "100");
FileStream fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, s);
fs.Close();
fs = new FileStream("temp.xml", FileMode.OpenOrCreate);
Without_Implementing_ISerializable d = (Without_Implementing_ISerializable)bf.Deserialize(fs);
fs.Close();
}
}
class Program
{
static void Main(string[] args)
{
Without_Implementing_ISerializable s = new Without_Implementing_ISerializable(5,"Five");
s.Check();
After_Implementing_ISerializable s1 = new After_Implementing_ISerializable(6, "Six");
s1.Check();
}
}
}
This is the error I got
"The constructor to deserialize an object of type 'Delete_This.After_Implementing_ISerializable' was not found."}
Yes, you need to implement a Serialization constructor for types implementing ISerializable; that serialization constructor is responsible for deserializing the object as it did for serialization using GetObjectData
method.
Serialization constructor will look something like this, with first parameter SerializationInfo
and second StreamingContext
.
protected ClassName(SerializationInfo info, StreamingContext context)
{
}
Remarks section in the provided link talks about this topic.
The ISerializable interface implies a constructor with the signature constructor (SerializationInfo information, StreamingContext context). At deserialization time, the current constructor is called only after the data in the SerializationInfo has been deserialized by the formatter. In general, this constructor should be protected if the class is not sealed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With