पर खाली है, मैं वर्तमान में एक बिडरेक्शनल मैप क्लास लिख रहा हूं, और मुझे कक्षा के क्रमिकरण/deserialization (नीचे सवाल) के साथ कुछ परेशानी हो रही है।शब्दकोश deserialization
यहां कक्षा के कुछ हिस्सों प्रासंगिक हैं।
/// <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);
}
}
आगे और inverseMap शब्दकोशों के बाद ठीक उसी डेटा होते हैं, मेरा विचार उनमें से केवल एक (forwardMap) क्रमानुसार करने, और फिर अक्रमांकन पर यह डेटा से अन्य (inverseMap) का निर्माण किया गया था। हालांकि, inverseMap deserialization कन्स्ट्रक्टर में किसी भी डेटा के साथ आबादी नहीं मिल रहा है। ऐसा लगता है कि फॉरवर्ड मैप डिक्शनरी केवल वर्ग के deserialization कन्स्ट्रक्टर पहले ही निष्पादित होने के बाद पूरी तरह से deserialized है।
इसे ठीक करने के तरीके पर कोई विचार?
बस पुष्टि करने के लिए, क्या आप ['बाइनरीफॉर्मेटर'] (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28v=vs.110% का उपयोग कर रहे हैं 29.aspx)? – dbc
क्या आप पूरा कोड पोस्ट कर सकते हैं ताकि हम इसे सीधे जांच सकें? –
पूर्ण कोड को संपादित और पोस्ट करने वाला था, लेकिन समस्या ठीक है जो @ डीबीसी नीचे बताई गई है। तो हाँ, मैं बाइनरीफॉर्मेटर का उपयोग कर रहा हूँ। – Henrik