diff --git a/src/Spring/Spring.Core/Collections/CaseInsensitiveHashtable.cs b/src/Spring/Spring.Core/Collections/CaseInsensitiveHashtable.cs index e838b3a8..1282cd64 100644 --- a/src/Spring/Spring.Core/Collections/CaseInsensitiveHashtable.cs +++ b/src/Spring/Spring.Core/Collections/CaseInsensitiveHashtable.cs @@ -1,5 +1,7 @@ +using System; using System.Collections; using System.Globalization; +using System.Runtime.Serialization; using Spring.Util; namespace Spring.Collections @@ -8,6 +10,7 @@ namespace Spring.Collections /// Provides a performance improved hashtable with case-insensitive (string-only! based) key handling. /// /// Erich Eichinger + [Serializable] public class CaseInsensitiveHashtable : Hashtable { private readonly TextInfo _textInfo; @@ -58,6 +61,35 @@ namespace Spring.Collections } } + /// + /// Initializes a new, empty instance of the class that is serializable using the specified and objects. + /// + /// A object containing the source and destination of the serialized stream associated with the . + /// A object containing the information required to serialize the object. + /// info is null. + protected CaseInsensitiveHashtable(SerializationInfo info, StreamingContext context) : base(info, context) + { + _textInfo = (TextInfo) info.GetValue("_textInfo", typeof(TextInfo)); + _compareInfo = (CompareInfo) info.GetValue("_compareInfo", typeof(CompareInfo)); + + AssertUtils.ArgumentNotNull(_textInfo, "TextInfo"); + AssertUtils.ArgumentNotNull(_compareInfo, "CompareInfo"); + } + + /// + ///Implements the interface and returns the data needed to serialize the . + /// + /// + ///A object containing the source and destination of the serialized stream associated with the . + ///A object containing the information required to serialize the . + ///info is null. + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + base.GetObjectData(info, context); + info.AddValue("_textInfo", _textInfo); + info.AddValue("_compareInfo", _compareInfo); + } + /// /// Calculate the hashcode of the given string key, using the configured culture. /// diff --git a/test/Spring/Spring.Core.Tests/Collections/CaseInsensitiveHashtableTests.cs b/test/Spring/Spring.Core.Tests/Collections/CaseInsensitiveHashtableTests.cs index e371b458..ba73e449 100644 --- a/test/Spring/Spring.Core.Tests/Collections/CaseInsensitiveHashtableTests.cs +++ b/test/Spring/Spring.Core.Tests/Collections/CaseInsensitiveHashtableTests.cs @@ -24,6 +24,8 @@ using System; using System.Collections; using System.Collections.Specialized; using System.Globalization; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; #endregion @@ -37,6 +39,36 @@ namespace Spring.Collections [TestFixture] public class CaseInsensitiveHashtableTests { + private static object SerializeDeserializeObject(object exp) + { + byte[] data; + BinaryFormatter formatter = new BinaryFormatter(); + using (MemoryStream ms = new MemoryStream()) + { + formatter.Serialize(ms, exp); + ms.Flush(); + data = ms.ToArray(); + } + + using (MemoryStream ms = new MemoryStream(data)) + { + exp = formatter.Deserialize(ms); + } + + return exp; + } + + [Test] + public void IsSerializable() + { + CaseInsensitiveHashtable storiginal = new CaseInsensitiveHashtable(); + storiginal.Add("key", "value"); + CaseInsensitiveHashtable st = (CaseInsensitiveHashtable)SerializeDeserializeObject(storiginal); + Assert.AreNotSame(storiginal, st); + Assert.AreEqual("value", st["KEY"]); + Assert.AreEqual(1, st.Count); + } + [Test] public void IgnoresCase() { @@ -53,18 +85,18 @@ namespace Spring.Collections Assert.Fail(); } catch (ArgumentException) - {} + { } Hashtable ht = new Hashtable(); - ht.Add("key","value"); - ht.Add("KEY","value"); + ht.Add("key", "value"); + ht.Add("KEY", "value"); try { st = new CaseInsensitiveHashtable(ht, CultureInfo.InvariantCulture); Assert.Fail(); } catch (ArgumentException) - {} + { } } [Test] @@ -93,7 +125,7 @@ namespace Spring.Collections StopWatch watch = new StopWatch(); Hashtable ht = CollectionsUtil.CreateCaseInsensitiveHashtable(); - for(int i=0;i<1000000;i++) ht.Add(Guid.NewGuid().ToString(), "val"); // gen. higher number of elements results in OOM exception???? + for (int i = 0; i < 1000000; i++) ht.Add(Guid.NewGuid().ToString(), "val"); // gen. higher number of elements results in OOM exception???? CaseInsensitiveHashtable ciht = new CaseInsensitiveHashtable(ht, CultureInfo.InvariantCulture); using (watch.Start("Duration: {0}")) @@ -102,7 +134,7 @@ namespace Spring.Collections { object v = ht["somekey"]; } - } + } using (watch.Start("Duration: {0}")) { @@ -110,7 +142,7 @@ namespace Spring.Collections { object v = ciht["somekey"]; } - } + } } } } \ No newline at end of file