[Serializable]
internal sealed class CultureAwareComparer : StringComparer
-#if FEATURE_RANDOMIZED_STRING_HASHING
- , IWellKnownStringEqualityComparer
-#endif
{
private readonly CompareInfo _compareInfo;
private readonly CompareOptions _options;
int hashCode = _compareInfo.GetHashCode();
return _options == CompareOptions.None ? hashCode : ~hashCode;
}
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
- IEqualityComparer IWellKnownStringEqualityComparer.GetEqualityComparerForSerialization() => this;
-#endif
}
[Serializable]
internal sealed class OrdinalComparer : StringComparer
-#if FEATURE_RANDOMIZED_STRING_HASHING
- , IWellKnownStringEqualityComparer
-#endif
{
public override int Compare(string x, string y) => string.CompareOrdinal(x, y);
// Equals/GetHashCode methods for the comparer itself.
public override bool Equals(object obj) => obj is OrdinalComparer;
public override int GetHashCode() => nameof(OrdinalComparer).GetHashCode();
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
- IEqualityComparer IWellKnownStringEqualityComparer.GetEqualityComparerForSerialization() => this;
-#endif
}
[Serializable]
internal sealed class OrdinalIgnoreCaseComparer : StringComparer
-#if FEATURE_RANDOMIZED_STRING_HASHING
- , IWellKnownStringEqualityComparer
-#endif
{
public override int Compare(string x, string y) => string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
// Equals/GetHashCode methods for the comparer itself.
public override bool Equals(object obj) => obj is OrdinalIgnoreCaseComparer;
public override int GetHashCode() => nameof(OrdinalIgnoreCaseComparer).GetHashCode();
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
- IEqualityComparer IWellKnownStringEqualityComparer.GetEqualityComparerForSerialization() => this;
-#endif
- }
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
- // This interface is implemented by string comparers in the framework that can opt into
- // randomized hashing behaviors.
- internal interface IWellKnownStringEqualityComparer
- {
- // Get an IEqaulityComparer that can be serailzied (e.g., it exists in older versions).
- IEqualityComparer GetEqualityComparerForSerialization();
}
-#endif
}
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.info);
}
info.AddValue(VersionName, version);
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
- info.AddValue(ComparerName, HashHelpers.GetEqualityComparerForSerialization(comparer), typeof(IEqualityComparer<TKey>));
-#else
info.AddValue(ComparerName, comparer, typeof(IEqualityComparer<TKey>));
-#endif
-
info.AddValue(HashSizeName, buckets == null ? 0 : buckets.Length); //This is the length of the bucket array.
if (buckets != null)
{
// If the comparer is null or a compatible comparer, serialize Hashtable
// in a format that can be deserialized on Everett and RTM.
//
- // Also, if the Hashtable is using randomized hashing, serialize the old
- // view of the _keycomparer so perevious frameworks don't see the new types
#pragma warning disable 618
-#if FEATURE_RANDOMIZED_STRING_HASHING
- IEqualityComparer keyComparerForSerilization = (IEqualityComparer)HashHelpers.GetEqualityComparerForSerialization(_keycomparer);
-#else
- IEqualityComparer keyComparerForSerilization = _keycomparer;
-#endif
+ IEqualityComparer keyComparerForSerilization = _keycomparer;
if (keyComparerForSerilization == null)
{
// This is the maximum prime smaller than Array.MaxArrayLength
public const int MaxPrimeArrayLength = 0x7FEFFFFD;
-
-#if FEATURE_RANDOMIZED_STRING_HASHING
-
- public static object GetEqualityComparerForSerialization(object comparer)
- {
- if (comparer == null)
- {
- return null;
- }
-
- IWellKnownStringEqualityComparer cmp = comparer as IWellKnownStringEqualityComparer;
-
- if (cmp != null)
- {
- return cmp.GetEqualityComparerForSerialization();
- }
-
- return comparer;
- }
-
- private const int bufferSize = 1024;
- private static int currentIndex = bufferSize;
- private static readonly object lockObj = new Object();
-#endif // FEATURE_RANDOMIZED_STRING_HASHING
}
}