Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class HashSet<T> : ICollection<T>, ISet<T>, IReadOnlyCollection<T>, IRead
private int _freeCount;
private int _version;
private IEqualityComparer<T>? _comparer;
private SerializationInfo? _siInfo; // temporary variable needed during deserialization

#region Constructors

Expand Down Expand Up @@ -137,7 +136,7 @@ protected HashSet(SerializationInfo info, StreamingContext context)
// deserialized and we have a reasonable estimate that GetHashCode is not going to
// fail. For the time being, we'll just cache this. The graph is not valid until
// OnDeserialization has been called.
_siInfo = info;
HashHelpers.SerializationInfoTable.Add(this, info);
}

/// <summary>Initializes the HashSet from another HashSet with the same element type and equality comparer.</summary>
Expand Down Expand Up @@ -411,16 +410,17 @@ public virtual void GetObjectData(SerializationInfo info, StreamingContext conte

public virtual void OnDeserialization(object? sender)
{
if (_siInfo == null)
HashHelpers.SerializationInfoTable.TryGetValue(this, out SerializationInfo? siInfo);
if (siInfo == null)
{
// It might be necessary to call OnDeserialization from a container if the
// container object also implements OnDeserialization. We can return immediately
// if this function is called twice. Note we set _siInfo to null at the end of this method.
return;
}

int capacity = _siInfo.GetInt32(CapacityName);
_comparer = (IEqualityComparer<T>)_siInfo.GetValue(ComparerName, typeof(IEqualityComparer<T>))!;
int capacity = siInfo.GetInt32(CapacityName);
_comparer = (IEqualityComparer<T>)siInfo.GetValue(ComparerName, typeof(IEqualityComparer<T>))!;
_freeList = -1;
_freeCount = 0;

Expand All @@ -432,7 +432,7 @@ public virtual void OnDeserialization(object? sender)
_fastModMultiplier = HashHelpers.GetFastModMultiplier((uint)capacity);
#endif

T[]? array = (T[]?)_siInfo.GetValue(ElementsName, typeof(T[]));
T[]? array = (T[]?)siInfo.GetValue(ElementsName, typeof(T[]));
if (array == null)
{
ThrowHelper.ThrowSerializationException(ExceptionResource.Serialization_MissingKeys);
Expand All @@ -449,8 +449,8 @@ public virtual void OnDeserialization(object? sender)
_buckets = null;
}

_version = _siInfo.GetInt32(VersionName);
_siInfo = null;
_version = siInfo.GetInt32(VersionName);
HashHelpers.SerializationInfoTable.Remove(this);
}

#endregion
Expand Down