Bug 2036 - Serialization support for Hashset
Summary: Serialization support for Hashset
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-11-15 10:59 UTC by Neale Ferguson
Modified: 2011-12-15 13:16 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case for proposed patch (1.64 KB, text/plain)
2011-11-15 11:00 UTC, Neale Ferguson
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Neale Ferguson 2011-11-15 10:59:50 UTC
I had a need to add serialization support for Hashset.cs. I based it on a combination of what I saw used elsewhere (Dictionary.cs, Hashtable.cs) as well as what was documented for .NET (i.e. what was being saved/restored). Here's the patch:

--- a/mcs/class/System.Core/System.Collections.Generic/HashSet.cs
+++ b/mcs/class/System.Core/System.Collections.Generic/HashSet.cs
@@ -572,22 +572,51 @@ namespace System.Collections.Generic {
 			return setComparer;
 		}
 
-		[MonoTODO]
 		[SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
 		public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
 		{
-			throw new NotImplementedException ();
+			if (info == null) {
+				throw new ArgumentNullException("info");
+			}
+			info.AddValue("Version", generation);
+			info.AddValue("Comparer", comparer, typeof(IEqualityComparer<T>));
+			info.AddValue("Capacity", (table == null) ? 0 : table.Length);
+			if (this.table != null) {
+				T[] tableArray = new T[table.Length];
+				CopyTo(tableArray);
+				info.AddValue("Elements", tableArray, typeof(T[]));
+			}
 		}
 
-		[MonoTODO]
 		public virtual void OnDeserialization (object sender)
 		{
-			if (si == null)
-				return;
+			if (si != null)
+			{
+				int generation = (int) si.GetValue("Version", typeof(int));
+				comparer = (IEqualityComparer<T>) si.GetValue("Comparer", 
+									      typeof(IEqualityComparer<T>));
+				int capacity = (int) si.GetValue("Capacity", typeof(int));
+
+				empty_slot = NO_SLOT;
+				if (capacity > 0) {
+					table = new int[capacity];
+					slots = new T[capacity];
+
+					T[] tableArray = (T[]) si.GetValue("Elements", typeof(T[]));
+					if (tableArray == null) 
+						throw new SerializationException("Missing Elements");
+
+					for (int iElement = 0; iElement < tableArray.Length; iElement++) {
+						Add(tableArray[iElement]);
+					}
+				} else 
+					table = null;
 
-			throw new NotImplementedException ();
+				si = null;
+			}
 		}
 
+
 		IEnumerator<T> IEnumerable<T>.GetEnumerator ()
 		{
 			return new Enumerator (this);

Attached is a test case that fails without the patch and works with it. I don't believe the testing has been robust enough for me to simply push this fix, so I'm hoping for comment and (constructive) criticism.
Comment 1 Neale Ferguson 2011-11-15 11:00:53 UTC
Created attachment 887 [details]
Test case for proposed patch

A simple test case for Hashset.cs serialization.
Comment 2 Neale Ferguson 2011-11-16 11:32:16 UTC
Revised patch (fixes using generation as a local variable):

--- a/mcs/class/System.Core/System.Collections.Generic/HashSet.cs
+++ b/mcs/class/System.Core/System.Collections.Generic/HashSet.cs
@@ -572,22 +572,51 @@ namespace System.Collections.Generic {
 			return setComparer;
 		}
 
-		[MonoTODO]
 		[SecurityPermission (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
 		public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
 		{
-			throw new NotImplementedException ();
+			if (info == null) {
+				throw new ArgumentNullException("info");
+			}
+			info.AddValue("Version", generation);
+			info.AddValue("Comparer", comparer, typeof(IEqualityComparer<T>));
+			info.AddValue("Capacity", (table == null) ? 0 : table.Length);
+			if (table != null) {
+				T[] tableArray = new T[table.Length];
+				CopyTo(tableArray);
+				info.AddValue("Elements", tableArray, typeof(T[]));
+			}
 		}
 
-		[MonoTODO]
 		public virtual void OnDeserialization (object sender)
 		{
-			if (si == null)
-				return;
+			if (si != null)
+			{
+				generation = (int) si.GetValue("Version", typeof(int));
+				comparer = (IEqualityComparer<T>) si.GetValue("Comparer", 
+									      typeof(IEqualityComparer<T>));
+				int capacity = (int) si.GetValue("Capacity", typeof(int));
+
+				empty_slot = NO_SLOT;
+				if (capacity > 0) {
+					table = new int[capacity];
+					slots = new T[capacity];
+
+					T[] tableArray = (T[]) si.GetValue("Elements", typeof(T[]));
+					if (tableArray == null) 
+						throw new SerializationException("Missing Elements");
+
+					for (int iElement = 0; iElement < tableArray.Length; iElement++) {
+						Add(tableArray[iElement]);
+					}
+				} else 
+					table = null;
 
-			throw new NotImplementedException ();
+				si = null;
+			}
 		}
 
+
 		IEnumerator<T> IEnumerable<T>.GetEnumerator ()
 		{
 			return new Enumerator (this);
Comment 3 Miguel de Icaza [MSFT] 2011-11-22 10:14:47 UTC
This patch has been applied.