From 77bab09bf568094ce5fb97a3200d99387b90b2a5 Mon Sep 17 00:00:00 2001 From: Luke Halliwell Date: Sun, 26 Dec 2010 08:16:50 -0800 Subject: [PATCH 1/3] Fix for bug 349053 - unable to serialize SortedDictionary --- .../SortedDictionary.cs | 34 ++++++++++-- .../SortedDictionaryTest.cs | 55 ++++++++++++++++++- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs index b535aa6881d9..e8354d32d9d1 100644 --- a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs +++ b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs @@ -35,13 +35,15 @@ using System; using System.Collections; using System.Diagnostics; +using System.Runtime.Serialization; +using System.Security.Permissions; namespace System.Collections.Generic { [Serializable] [DebuggerDisplay ("Count={Count}")] [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))] - public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable + public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable, ISerializable { class Node : RBTree.Node { public TKey key; @@ -76,6 +78,7 @@ public DictionaryEntry AsDE () } } + [Serializable] class NodeHelper : RBTree.INodeHelper { public IComparer cmp; @@ -127,6 +130,17 @@ public SortedDictionary (IDictionary dic, IComparer comparer) foreach (KeyValuePair entry in dic) Add (entry.Key, entry.Value); } + + protected SortedDictionary (SerializationInfo info, StreamingContext context) + { + hlp = (NodeHelper)info.GetValue("Helper", typeof(NodeHelper)); + tree = new RBTree (hlp); + + KeyValuePair [] data = (KeyValuePair[])info.GetValue("KeyValuePairs", typeof(KeyValuePair[])); + foreach (KeyValuePair entry in data) + Add(entry.Key, entry.Value); + } + #endregion #region PublicProperty @@ -226,6 +240,18 @@ public bool TryGetValue (TKey key, out TValue value) return n != null; } + [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)] + public virtual void GetObjectData (SerializationInfo info, StreamingContext context) + { + if (info == null) + throw new ArgumentNullException ("info"); + + KeyValuePair [] data = new KeyValuePair [Count]; + CopyTo (data, 0); + info.AddValue ("KeyValuePairs", data); + info.AddValue ("Helper", hlp); + } + #endregion #region PrivateMethod @@ -280,7 +306,7 @@ bool ICollection>.Remove (KeyValuePair it { TValue value; return TryGetValue (item.Key, out value) && - EqualityComparer.Default.Equals (item.Value, value) && + EqualityComparer.Default.Equals (item.Value, value) && Remove (item.Key); } @@ -466,7 +492,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator,IEnumerator, IDisposable @@ -601,7 +627,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator, IEnumerator, IDisposable diff --git a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs index 5fd1f6bcd1df..bb1c225f63f0 100644 --- a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs +++ b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs @@ -29,16 +29,17 @@ #if NET_2_0 using System; +using System.IO; using System.Collections; using System.Collections.Generic; -using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; namespace MonoTests.System.Collections.Generic { [TestFixture] - public class SortedDictionaryTest + public class SortedDictionaryTest { [Test] public void CtorNullComparer () @@ -565,8 +566,58 @@ public void ValueEnumerator_Current () ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); } + + // Serialize a dictionary out and deserialize it back in again + SortedDictionary Roundtrip(SortedDictionary dic) + { + BinaryFormatter bf = new BinaryFormatter (); + MemoryStream stream = new MemoryStream (); + bf.Serialize (stream, dic); + stream.Position = 0; + return (SortedDictionary)bf.Deserialize (stream); + } + + [Test] + public void Serialize() + { + SortedDictionary test = new SortedDictionary(); + test.Add(1, "a"); + test.Add(3, "c"); + test.Add(2, "b"); + + SortedDictionary result = Roundtrip(test); + Assert.AreEqual(3, result.Count); + + Assert.AreEqual("a", result[1]); + Assert.AreEqual("b", result[2]); + Assert.AreEqual("c", result[3]); + } + + [Test] + public void SerializeReverseComparer() + { + SortedDictionary test = + new SortedDictionary ( + ReverseComparer.Instance); + + test.Add (1, "A"); + test.Add (3, "B"); + test.Add (2, "C"); + + SortedDictionary result = Roundtrip (test); + + SortedDictionary.Enumerator e = result.GetEnumerator (); + Assert.IsTrue (e.MoveNext (), "#1"); + Assert.AreEqual ("B", e.Current.Value, "#2"); + Assert.IsTrue (e.MoveNext (), "#3"); + Assert.AreEqual ("C", e.Current.Value, "#4"); + Assert.IsTrue (e.MoveNext (), "#5"); + Assert.AreEqual ("A", e.Current.Value, "#6"); + Assert.IsFalse (e.MoveNext (), "#7"); + } } + [Serializable] class ReverseComparer : IComparer { static ReverseComparer instance = new ReverseComparer (); From 8401c264d1e0e2444143d810a0a1998073d09993 Mon Sep 17 00:00:00 2001 From: Luke Halliwell Date: Sun, 26 Dec 2010 08:18:10 -0800 Subject: [PATCH 2/3] Revert "Fix for bug 349053 - unable to serialize SortedDictionary" This reverts commit 77bab09bf568094ce5fb97a3200d99387b90b2a5. Accidentally added some files. --- .../SortedDictionary.cs | 34 ++---------- .../SortedDictionaryTest.cs | 55 +------------------ 2 files changed, 6 insertions(+), 83 deletions(-) diff --git a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs index e8354d32d9d1..b535aa6881d9 100644 --- a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs +++ b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs @@ -35,15 +35,13 @@ using System; using System.Collections; using System.Diagnostics; -using System.Runtime.Serialization; -using System.Security.Permissions; namespace System.Collections.Generic { [Serializable] [DebuggerDisplay ("Count={Count}")] [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))] - public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable, ISerializable + public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable { class Node : RBTree.Node { public TKey key; @@ -78,7 +76,6 @@ public DictionaryEntry AsDE () } } - [Serializable] class NodeHelper : RBTree.INodeHelper { public IComparer cmp; @@ -130,17 +127,6 @@ public SortedDictionary (IDictionary dic, IComparer comparer) foreach (KeyValuePair entry in dic) Add (entry.Key, entry.Value); } - - protected SortedDictionary (SerializationInfo info, StreamingContext context) - { - hlp = (NodeHelper)info.GetValue("Helper", typeof(NodeHelper)); - tree = new RBTree (hlp); - - KeyValuePair [] data = (KeyValuePair[])info.GetValue("KeyValuePairs", typeof(KeyValuePair[])); - foreach (KeyValuePair entry in data) - Add(entry.Key, entry.Value); - } - #endregion #region PublicProperty @@ -240,18 +226,6 @@ public bool TryGetValue (TKey key, out TValue value) return n != null; } - [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)] - public virtual void GetObjectData (SerializationInfo info, StreamingContext context) - { - if (info == null) - throw new ArgumentNullException ("info"); - - KeyValuePair [] data = new KeyValuePair [Count]; - CopyTo (data, 0); - info.AddValue ("KeyValuePairs", data); - info.AddValue ("Helper", hlp); - } - #endregion #region PrivateMethod @@ -306,7 +280,7 @@ bool ICollection>.Remove (KeyValuePair it { TValue value; return TryGetValue (item.Key, out value) && - EqualityComparer.Default.Equals (item.Value, value) && + EqualityComparer.Default.Equals (item.Value, value) && Remove (item.Key); } @@ -492,7 +466,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator,IEnumerator, IDisposable @@ -627,7 +601,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator, IEnumerator, IDisposable diff --git a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs index bb1c225f63f0..5fd1f6bcd1df 100644 --- a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs +++ b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs @@ -29,17 +29,16 @@ #if NET_2_0 using System; -using System.IO; using System.Collections; using System.Collections.Generic; -using System.Runtime.Serialization.Formatters.Binary; +using System.Runtime.Serialization; using NUnit.Framework; namespace MonoTests.System.Collections.Generic { [TestFixture] - public class SortedDictionaryTest + public class SortedDictionaryTest { [Test] public void CtorNullComparer () @@ -566,58 +565,8 @@ public void ValueEnumerator_Current () ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); } - - // Serialize a dictionary out and deserialize it back in again - SortedDictionary Roundtrip(SortedDictionary dic) - { - BinaryFormatter bf = new BinaryFormatter (); - MemoryStream stream = new MemoryStream (); - bf.Serialize (stream, dic); - stream.Position = 0; - return (SortedDictionary)bf.Deserialize (stream); - } - - [Test] - public void Serialize() - { - SortedDictionary test = new SortedDictionary(); - test.Add(1, "a"); - test.Add(3, "c"); - test.Add(2, "b"); - - SortedDictionary result = Roundtrip(test); - Assert.AreEqual(3, result.Count); - - Assert.AreEqual("a", result[1]); - Assert.AreEqual("b", result[2]); - Assert.AreEqual("c", result[3]); - } - - [Test] - public void SerializeReverseComparer() - { - SortedDictionary test = - new SortedDictionary ( - ReverseComparer.Instance); - - test.Add (1, "A"); - test.Add (3, "B"); - test.Add (2, "C"); - - SortedDictionary result = Roundtrip (test); - - SortedDictionary.Enumerator e = result.GetEnumerator (); - Assert.IsTrue (e.MoveNext (), "#1"); - Assert.AreEqual ("B", e.Current.Value, "#2"); - Assert.IsTrue (e.MoveNext (), "#3"); - Assert.AreEqual ("C", e.Current.Value, "#4"); - Assert.IsTrue (e.MoveNext (), "#5"); - Assert.AreEqual ("A", e.Current.Value, "#6"); - Assert.IsFalse (e.MoveNext (), "#7"); - } } - [Serializable] class ReverseComparer : IComparer { static ReverseComparer instance = new ReverseComparer (); From 806ae4b30b59d809a2b1b35a3b383f7017a5bfe8 Mon Sep 17 00:00:00 2001 From: Luke Halliwell Date: Sun, 26 Dec 2010 08:26:44 -0800 Subject: [PATCH 3/3] Fix for bug 349053 - unable to serialize SortedDictionary --- .../SortedDictionary.cs | 34 ++++++++++-- .../SortedDictionaryTest.cs | 55 ++++++++++++++++++- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs index b535aa6881d9..e8354d32d9d1 100644 --- a/mcs/class/System/System.Collections.Generic/SortedDictionary.cs +++ b/mcs/class/System/System.Collections.Generic/SortedDictionary.cs @@ -35,13 +35,15 @@ using System; using System.Collections; using System.Diagnostics; +using System.Runtime.Serialization; +using System.Security.Permissions; namespace System.Collections.Generic { [Serializable] [DebuggerDisplay ("Count={Count}")] [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))] - public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable + public class SortedDictionary : IDictionary, ICollection>, IEnumerable>, IDictionary, ICollection, IEnumerable, ISerializable { class Node : RBTree.Node { public TKey key; @@ -76,6 +78,7 @@ public DictionaryEntry AsDE () } } + [Serializable] class NodeHelper : RBTree.INodeHelper { public IComparer cmp; @@ -127,6 +130,17 @@ public SortedDictionary (IDictionary dic, IComparer comparer) foreach (KeyValuePair entry in dic) Add (entry.Key, entry.Value); } + + protected SortedDictionary (SerializationInfo info, StreamingContext context) + { + hlp = (NodeHelper)info.GetValue("Helper", typeof(NodeHelper)); + tree = new RBTree (hlp); + + KeyValuePair [] data = (KeyValuePair[])info.GetValue("KeyValuePairs", typeof(KeyValuePair[])); + foreach (KeyValuePair entry in data) + Add(entry.Key, entry.Value); + } + #endregion #region PublicProperty @@ -226,6 +240,18 @@ public bool TryGetValue (TKey key, out TValue value) return n != null; } + [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)] + public virtual void GetObjectData (SerializationInfo info, StreamingContext context) + { + if (info == null) + throw new ArgumentNullException ("info"); + + KeyValuePair [] data = new KeyValuePair [Count]; + CopyTo (data, 0); + info.AddValue ("KeyValuePairs", data); + info.AddValue ("Helper", hlp); + } + #endregion #region PrivateMethod @@ -280,7 +306,7 @@ bool ICollection>.Remove (KeyValuePair it { TValue value; return TryGetValue (item.Key, out value) && - EqualityComparer.Default.Equals (item.Value, value) && + EqualityComparer.Default.Equals (item.Value, value) && Remove (item.Key); } @@ -466,7 +492,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator,IEnumerator, IDisposable @@ -601,7 +627,7 @@ object ICollection.SyncRoot { IEnumerator IEnumerable.GetEnumerator () { - return new Enumerator (_dic); + return new Enumerator (_dic); } public struct Enumerator : IEnumerator, IEnumerator, IDisposable diff --git a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs index 5fd1f6bcd1df..bb1c225f63f0 100644 --- a/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs +++ b/mcs/class/System/Test/System.Collections.Generic/SortedDictionaryTest.cs @@ -29,16 +29,17 @@ #if NET_2_0 using System; +using System.IO; using System.Collections; using System.Collections.Generic; -using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; namespace MonoTests.System.Collections.Generic { [TestFixture] - public class SortedDictionaryTest + public class SortedDictionaryTest { [Test] public void CtorNullComparer () @@ -565,8 +566,58 @@ public void ValueEnumerator_Current () ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); } + + // Serialize a dictionary out and deserialize it back in again + SortedDictionary Roundtrip(SortedDictionary dic) + { + BinaryFormatter bf = new BinaryFormatter (); + MemoryStream stream = new MemoryStream (); + bf.Serialize (stream, dic); + stream.Position = 0; + return (SortedDictionary)bf.Deserialize (stream); + } + + [Test] + public void Serialize() + { + SortedDictionary test = new SortedDictionary(); + test.Add(1, "a"); + test.Add(3, "c"); + test.Add(2, "b"); + + SortedDictionary result = Roundtrip(test); + Assert.AreEqual(3, result.Count); + + Assert.AreEqual("a", result[1]); + Assert.AreEqual("b", result[2]); + Assert.AreEqual("c", result[3]); + } + + [Test] + public void SerializeReverseComparer() + { + SortedDictionary test = + new SortedDictionary ( + ReverseComparer.Instance); + + test.Add (1, "A"); + test.Add (3, "B"); + test.Add (2, "C"); + + SortedDictionary result = Roundtrip (test); + + SortedDictionary.Enumerator e = result.GetEnumerator (); + Assert.IsTrue (e.MoveNext (), "#1"); + Assert.AreEqual ("B", e.Current.Value, "#2"); + Assert.IsTrue (e.MoveNext (), "#3"); + Assert.AreEqual ("C", e.Current.Value, "#4"); + Assert.IsTrue (e.MoveNext (), "#5"); + Assert.AreEqual ("A", e.Current.Value, "#6"); + Assert.IsFalse (e.MoveNext (), "#7"); + } } + [Serializable] class ReverseComparer : IComparer { static ReverseComparer instance = new ReverseComparer ();