G
Guest
I've got this strange problem in a class that uses ISerializable to serialize a NameValueCollection
During deserialization, in the object constructor, after using GetValue to get the NameValueCollection from SerializationInfo, the NameValueCollection is not usable. It throws a NullReferenceException from pretty much any property/method. It is usable after the object is deserialized, but not in the constructor. I've narrowed down the code to a small sample. Can an object not be used during the deserialization constructor? I haven't found any docs that say this shouldn't be allowed
Code sample
================Snip==============
using System
using System.Collections.Specialized
using System.IO
using System.Runtime.Serialization
using System.Runtime.Serialization.Formatters.Binary
using System.Runtime.Serialization.Formatters.Soap
namespace SerializationTes
[Serializable
public class SerializeMe : ISerializabl
private NameValueCollection values
public SerializeMe()
values = new NameValueCollection()
values.Add("string1", "hi")
values.Add("string2", "there")
protected SerializeMe(SerializationInfo info, StreamingContext context)
values = (NameValueCollection) info.GetValue("values", typeof(NameValueCollection))
PrintMe(); // <-- why can this not access values??
// throws null reference exception inside values.get_Count(
static void Main(string[] args
// create one, print i
SerializeMe serializeme = new SerializeMe()
serializeme.PrintMe()
// serialize it to fil
FileStream file = new FileStream("c:\\test.txt", FileMode.Create)
SoapFormatter formatter = new SoapFormatter()
formatter.Serialize(file, serializeme)
file.Close()
serializeme = null
// read it back and try to prin
file = new FileStream("c:\\test.txt", FileMode.Open)
try
serializeme = (SerializeMe) formatter.Deserialize(file)
serializeme.PrintMe(); // <-- this works, what is different here than print in constructor
} catch(Exception e)
Console.WriteLine("Failed: " + e)
file.Close()
public void PrintMe()
Console.WriteLine("Items: " + values.Count)
foreach(string sKey in values.Keys)
Console.WriteLine("Key: " + sKey + ": " + values[sKey])
public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue("values", values)
During deserialization, in the object constructor, after using GetValue to get the NameValueCollection from SerializationInfo, the NameValueCollection is not usable. It throws a NullReferenceException from pretty much any property/method. It is usable after the object is deserialized, but not in the constructor. I've narrowed down the code to a small sample. Can an object not be used during the deserialization constructor? I haven't found any docs that say this shouldn't be allowed
Code sample
================Snip==============
using System
using System.Collections.Specialized
using System.IO
using System.Runtime.Serialization
using System.Runtime.Serialization.Formatters.Binary
using System.Runtime.Serialization.Formatters.Soap
namespace SerializationTes
[Serializable
public class SerializeMe : ISerializabl
private NameValueCollection values
public SerializeMe()
values = new NameValueCollection()
values.Add("string1", "hi")
values.Add("string2", "there")
protected SerializeMe(SerializationInfo info, StreamingContext context)
values = (NameValueCollection) info.GetValue("values", typeof(NameValueCollection))
PrintMe(); // <-- why can this not access values??
// throws null reference exception inside values.get_Count(
static void Main(string[] args
// create one, print i
SerializeMe serializeme = new SerializeMe()
serializeme.PrintMe()
// serialize it to fil
FileStream file = new FileStream("c:\\test.txt", FileMode.Create)
SoapFormatter formatter = new SoapFormatter()
formatter.Serialize(file, serializeme)
file.Close()
serializeme = null
// read it back and try to prin
file = new FileStream("c:\\test.txt", FileMode.Open)
try
serializeme = (SerializeMe) formatter.Deserialize(file)
serializeme.PrintMe(); // <-- this works, what is different here than print in constructor
} catch(Exception e)
Console.WriteLine("Failed: " + e)
file.Close()
public void PrintMe()
Console.WriteLine("Items: " + values.Count)
foreach(string sKey in values.Keys)
Console.WriteLine("Key: " + sKey + ": " + values[sKey])
public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue("values", values)