Tôi hiện đang viết một lớp bản đồ hai chiều, và tôi đang gặp một số rắc rối với việc tuần tự hóa/deserialization của lớp (câu hỏi ở dưới).Từ điển trống trên deserialization
Đây là các phần của lớp học có liên quan.
/// <summary>
/// Represents a dictionary where both keys and values are unique, and the mapping between them is bidirectional.
/// </summary>
/// <typeparam name="TKey"> The type of the keys in the dictionary. </typeparam>
/// <typeparam name="TValue"> The type of the values in the dictionary. </typeparam>
[Serializable]
public class BidirectionalDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IEquatable<BidirectionalDictionary<TKey, TValue>>, ISerializable, IDeserializationCallback
{
/// <summary>
/// A dictionary that maps the keys to values.
/// </summary>
private readonly Dictionary<TKey, TValue> forwardMap;
/// <summary>
/// A dictionary that maps the values to keys.
/// </summary>
private readonly Dictionary<TValue, TKey> inverseMap;
/// <summary>
/// An instance of the dictionary where the values are the keys, and the keys are the values.
/// </summary>
private readonly BidirectionalDictionary<TValue, TKey> inverseInstance;
/// <summary>
/// Initializes a new instance of the dictionary class with serialized data. </summary>
/// </summary>
/// <param name="info"> The serialization info. </param>
/// <param name="context"> The sserialization context. </param>
protected BidirectionalDictionary(SerializationInfo info, StreamingContext context)
{
this.forwardMap = (Dictionary<TKey, TValue>)info.GetValue("UnderlyingDictionary", typeof(Dictionary<TKey, TValue>));
this.inverseMap = new Dictionary<TValue, TKey>(
forwardMap.Count,
(IEqualityComparer<TValue>)info.GetValue("InverseComparer", typeof(IEqualityComparer<TValue>)));
// forwardMap is always empty at this point.
foreach (KeyValuePair<TKey, TValue> entry in forwardMap)
inverseMap.Add(entry.Value, entry.Key);
this.inverseInstance = new BidirectionalDictionary<TValue, TKey>(this);
}
/// <summary>
/// Gets the data needed to serialize the dictionary.
/// </summary>
/// <param name="info"> The serialization info. </param>
/// <param name="context"> The serialization context. </param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("UnderlyingDictionary", forwardMap);
info.AddValue("InverseComparer", inverseMap.Comparer);
}
}
Từ điển tiên tiến hơn và inverseMap chứa các dữ liệu chính xác cùng, ý tưởng của tôi là để chỉ serialize một trong số họ (forwardMap), và sau đó xây dựng khác (inverseMap) từ dữ liệu của nó trên deserialization. Tuy nhiên, inverseMap không nhận được dân cư với bất kỳ dữ liệu nào trong hàm tạo deserialization. Có vẻ như từ điển forwardMap chỉ hoàn toàn deserialized sau khi constructor deserialization của lớp đã thực hiện.
Bất kỳ ý tưởng nào về cách sửa lỗi này?
chỉ để xác nhận, bạn đang sử dụng ['BinaryFormatter'] (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28v=vs.110% 29.aspx)? – dbc
Bạn có thể vui lòng đăng toàn bộ mã để chúng tôi có thể kiểm tra trực tiếp không? –
Đã sắp sửa chỉnh sửa và đăng toàn bộ mã, nhưng vấn đề chính xác là những gì @dbc được giải thích dưới đây. Vì vậy, yeah, tôi đang sử dụng BinaryFormatter. – Henrik