.NET 2.x Почему BinaryFormatter не сериализует мой класс? - C#
Формулировка задачи:
Здравствуйте. Проблема в том, что BinaryFormatter не сериализует этот класс (необходим мне для Unity, так как там проблема с дессереализацией KeyValuePair):
При серриализации даже не обращается к serializationData.
.NET 2.0.
[Serializable]
public class SerializableDictionary<TKey, TVal>
{
[Serializable]
private class _KeyValuePair<TKey, TVal>
{
public TKey Key;
public TVal Value;
}
private _KeyValuePair<TKey, TVal>[] serializationData
{
get
{
int count = dictionary.Count;
_KeyValuePair<TKey, TVal>[] kv = new _KeyValuePair<TKey, TVal>[count];
if(count > 0)
{
IEnumerator<KeyValuePair<TKey, TVal>> ie = dictionary.GetEnumerator();
int i = 0;
while(ie.MoveNext())
{
_KeyValuePair<TKey, TVal> element = new _KeyValuePair<TKey, TVal>();
element.Key = ie.Current.Key;
element.Value = ie.Current.Value;
kv[i] = element;
i++;
}
}
return kv;
}
set
{
if(dictionary == null)
{
dictionary = new Dictionary<TKey, TVal>();
}
_KeyValuePair<TKey, TVal>[] kv = value;
if(kv.Length > 0)
{
for(int i = 0; i < kv.Length; i++)
{
dictionary.Add(kv[i].Key, kv[i].Value);
}
}
}
}
[NonSerialized]
public Dictionary<TKey, TVal> dictionary;
public SerializableDictionary()
{
dictionary = new Dictionary<TKey, TVal>();
}
public TVal this[TKey key]
{
get
{
return dictionary[key];
}
set
{
dictionary[key] = value;
}
}
public bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public void Add(TKey key, TVal value)
{
dictionary.Add(key, value);
}
public bool TryGetValue(TKey key, out TVal value)
{
return dictionary.TryGetValue(key, out value);
}
public void Remove(TKey key)
{
dictionary.Remove(key);
}
}Решение задачи: «.NET 2.x Почему BinaryFormatter не сериализует мой класс?»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace BuildingLibrary
{
[Serializable()]
public class SerializableDictionary<TKey, TVal> : ISerializable
{
[Serializable]
protected class _KeyValuePair<TKey, TVal>
{
public TKey Key;
public TVal Value;
public _KeyValuePair()
{ }
}
protected _KeyValuePair<TKey, TVal>[] serializationData
{
get
{
int count = dictionary.Count;
_KeyValuePair<TKey, TVal>[] kv = new _KeyValuePair<TKey, TVal>[count];
if(count > 0)
{
IEnumerator<KeyValuePair<TKey, TVal>> ie = dictionary.GetEnumerator();
int i = 0;
while(ie.MoveNext())
{
_KeyValuePair<TKey, TVal> element = new _KeyValuePair<TKey, TVal>();
element.Key = ie.Current.Key;
element.Value = ie.Current.Value;
kv[i] = element;
i++;
}
}
return kv;
}
set
{
if(dictionary == null)
{
dictionary = new Dictionary<TKey, TVal>();
}
_KeyValuePair<TKey, TVal>[] kv = value;
if(kv.Length > 0)
{
for(int i = 0; i < kv.Length; i++)
{
dictionary.Add(kv[i].Key, kv[i].Value);
}
}
}
}
[NonSerialized]
public Dictionary<TKey, TVal> dictionary;
public SerializableDictionary()
{
dictionary = new Dictionary<TKey, TVal>();
}
protected SerializableDictionary(SerializationInfo info, StreamingContext context)
{
serializationData = (_KeyValuePair<TKey, TVal>[])info.GetValue("serializationData", typeof(_KeyValuePair<TKey, TVal>[]));
}
public TVal this[TKey key]
{
get
{
return dictionary[key];
}
set
{
dictionary[key] = value;
}
}
public bool ContainsKey(TKey key)
{
return dictionary.ContainsKey(key);
}
public void Add(TKey key, TVal value)
{
dictionary.Add(key, value);
}
public bool TryGetValue(TKey key, out TVal value)
{
return dictionary.TryGetValue(key, out value);
}
public void Remove(TKey key)
{
dictionary.Remove(key);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("serializationData", serializationData, typeof(_KeyValuePair<TKey, TVal>[]));
}
}
}