Почему Dictionary десериализуется в последнюю очередь и как с этим бороться? Мне надо сделать словарь, у которого ключ — тоже словарь, а я не могу его десериализовать
При десериализации в конструкторе MyDictionary извлекаемые MyKey пустые! И заполняются только уже после всей десериализации! Вот и вылетает исключение, что такой элемень уже добавлен в словарь! Как же это тогда десериализовать?!
[Serializable]
class MyKey : Dictionary<int, int>
{
public MyKey(params int[] nums)
{
foreach (int i in nums)
{
Add(i, 0);
}
}
protected MyKey(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
MyKey key = (MyKey)obj;
if (key.Count != Count)
{
return false;
}
Enumerator enumerator1 = GetEnumerator();
Enumerator enumerator2 = key.GetEnumerator();
while (enumerator1.MoveNext())
{
enumerator2.MoveNext();
if (!enumerator1.Current.Key.Equals(enumerator2.Current.Key) ||
!enumerator1.Current.Value.Equals(enumerator2.Current.Value))
{
return false;
}
}
return true;
}
public override int GetHashCode()
{
int hash = 0;
foreach (KeyValuePair<int, int> pair in this)
{
hash ^= pair.Key ^ pair.Value;
}
return hash;
}
}
[Serializable]
class MyDictionary : Dictionary<MyKey, int>
{
public MyDictionary()
{
}
protected MyDictionary(SerializationInfo info, StreamingContext context)
{
int count = info.GetInt32("count");
for (int i = 0; i < count; ++i)
{
MyKey key = (MyKey)info.GetValue(i + "key", typeof(MyKey));
int value = info.GetInt32(i + "value");
Add(key, value);
}
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
int i = 0;
foreach (KeyValuePair<MyKey, int> pair in this)
{
info.AddValue(i + "key", pair.Key);
info.AddValue(i + "value", pair.Value);
++i;
}
info.AddValue("count", i);
}
}
//[Serializable]
//class MyDictionary : List<MyKey>, ISerializable
//{
// public MyDictionary()
// {
// }
// protected MyDictionary(SerializationInfo info, StreamingContext context)
// {
// int count = info.GetInt32("count");
// for (int i = 0; i < count; ++i)
// {
// MyKey key = (MyKey)info.GetValue(i.ToString(), typeof(MyKey));
// if (Contains(key))
// {
// throw new NotSupportedException();
// }
// Add(key);
// }
// }
// public void GetObjectData(SerializationInfo info, StreamingContext context)
// {
// int i = 0;
// foreach (MyKey key in this)
// {
// info.AddValue(i.ToString(), key);
// ++i;
// }
// info.AddValue("count", i);
// }
//}
[Test]
public void DelayedDeserializationTest()
{
MyKey key1 = new MyKey();
key1.Add(1, 2);
key1.Add(2, 1);
MyKey key2 = new MyKey();
key2.Add(1, 2);
key2.Add(2, 1);
Assert.AreEqual(key1, key2);
key2.Add(3, 2);
Assert.AreNotEqual(key1, key2);
MyDictionary dic = new MyDictionary();
dic.Add(new MyKey(0, 1, 2), 0);
dic.Add(new MyKey(1, 2, 3), 0);
dic.Add(new MyKey(2, 3, 4), 0);
//dic.Add(new MyKey(0, 1, 2));
//dic.Add(new MyKey(1, 2, 3));
//dic.Add(new MyKey(2, 3, 4));
using (FileStream outSteam = new FileStream("1.tmp", FileMode.Create))
{
formatter.Serialize(outSteam, dic);
}
using (FileStream inStream = new FileStream("1.tmp", FileMode.Open))
{
formatter.Deserialize(inStream);
}
}