D
Daniel Gackle
I'm getting a strange ArgumentNullException after deserializing a
SortedList. Haven't seen this discussed in the newsgroups, but it
looks like a bug - unless I missed something obvious? I've distilled
the problem into the following code. Can anybody reproduce/explain the
problem?
Thanks,
Daniel
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
[Serializable]
class Class1 : ISerializable
{
private SortedList sortedListMember;
private DateTime sampleDate = new DateTime(1999, 9, 9);
public Class1()
{
sortedListMember = new SortedList();
sortedListMember[sampleDate] = 4.56;
}
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("sortedListMember", sortedListMember);
}
public Class1(SerializationInfo info, StreamingContext context)
{
readSortedList(info);
}
private void readSortedList(SerializationInfo info)
{
try
{
SortedList list = (SortedList)
info.GetValue("sortedListMember", typeof(SortedList));
// **** Executing this line causes an ArgumentNullException
****
object element = list[sampleDate];
}
catch (Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.ReadLine();
}
}
[STAThread]
static void Main(string[] args)
{
using (MemoryStream stream = new MemoryStream())
{
// serialize
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, new Class1());
// go back to start before deserializing
stream.Position = 0;
// deserialize
bf = new BinaryFormatter();
bf.Deserialize(stream);
}
}
}
}
SortedList. Haven't seen this discussed in the newsgroups, but it
looks like a bug - unless I missed something obvious? I've distilled
the problem into the following code. Can anybody reproduce/explain the
problem?
Thanks,
Daniel
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApplication1
{
[Serializable]
class Class1 : ISerializable
{
private SortedList sortedListMember;
private DateTime sampleDate = new DateTime(1999, 9, 9);
public Class1()
{
sortedListMember = new SortedList();
sortedListMember[sampleDate] = 4.56;
}
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("sortedListMember", sortedListMember);
}
public Class1(SerializationInfo info, StreamingContext context)
{
readSortedList(info);
}
private void readSortedList(SerializationInfo info)
{
try
{
SortedList list = (SortedList)
info.GetValue("sortedListMember", typeof(SortedList));
// **** Executing this line causes an ArgumentNullException
****
object element = list[sampleDate];
}
catch (Exception e)
{
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.ReadLine();
}
}
[STAThread]
static void Main(string[] args)
{
using (MemoryStream stream = new MemoryStream())
{
// serialize
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, new Class1());
// go back to start before deserializing
stream.Position = 0;
// deserialize
bf = new BinaryFormatter();
bf.Deserialize(stream);
}
}
}
}