diff options
Diffstat (limited to 'src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs')
-rw-r--r-- | src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs b/src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs index 0cd1bc1c12..d6b213c9d7 100644 --- a/src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs +++ b/src/mscorlib/src/System/Collections/Generic/EqualityComparer.cs @@ -10,6 +10,7 @@ using System.Security; using System.Globalization; using System.Runtime; using System.Runtime.CompilerServices; +using System.Runtime.Serialization; using System.Diagnostics.Contracts; namespace System.Collections.Generic @@ -352,7 +353,7 @@ namespace System.Collections.Generic } [Serializable] - internal class EnumEqualityComparer<T> : EqualityComparer<T> where T : struct + internal class EnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct { [Pure] public override bool Equals(T x, T y) @@ -371,6 +372,9 @@ namespace System.Collections.Generic public EnumEqualityComparer() { } + // This is used by the serialization engine. + protected EnumEqualityComparer(SerializationInfo information, StreamingContext context) { } + // Equals method for the comparer itself. public override bool Equals(Object obj) => obj != null && GetType() == obj.GetType(); @@ -401,6 +405,14 @@ namespace System.Collections.Generic } return -1; } + + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + // For back-compat we need to serialize the comparers for enums with underlying types other than int as ObjectEqualityComparer + if (Type.GetTypeCode(Enum.GetUnderlyingType(typeof(T))) != TypeCode.Int32) { + info.SetType(typeof(ObjectEqualityComparer<T>)); + } + } } [Serializable] @@ -408,6 +420,9 @@ namespace System.Collections.Generic { public SByteEnumEqualityComparer() { } + // This is used by the serialization engine. + public SByteEnumEqualityComparer(SerializationInfo information, StreamingContext context) { } + [Pure] public override int GetHashCode(T obj) { @@ -417,10 +432,13 @@ namespace System.Collections.Generic } [Serializable] - internal sealed class ShortEnumEqualityComparer<T> : EnumEqualityComparer<T> where T : struct + internal sealed class ShortEnumEqualityComparer<T> : EnumEqualityComparer<T>, ISerializable where T : struct { public ShortEnumEqualityComparer() { } + // This is used by the serialization engine. + public ShortEnumEqualityComparer(SerializationInfo information, StreamingContext context) { } + [Pure] public override int GetHashCode(T obj) { @@ -430,7 +448,7 @@ namespace System.Collections.Generic } [Serializable] - internal sealed class LongEnumEqualityComparer<T> : EqualityComparer<T> where T : struct + internal sealed class LongEnumEqualityComparer<T> : EqualityComparer<T>, ISerializable where T : struct { [Pure] public override bool Equals(T x, T y) @@ -479,5 +497,15 @@ namespace System.Collections.Generic } return -1; } + + // This is used by the serialization engine. + public LongEnumEqualityComparer(SerializationInfo information, StreamingContext context) { } + + public void GetObjectData(SerializationInfo info, StreamingContext context) + { + // The LongEnumEqualityComparer does not exist on 4.0 so we need to serialize this comparer as ObjectEqualityComparer + // to allow for roundtrip between 4.0 and 4.5. + info.SetType(typeof(ObjectEqualityComparer<T>)); + } } } |