Xamarin Public Jenkins (auto-signing) ef583813eb Imported Upstream version 6.4.0.137
Former-commit-id: 943baa9f16a098c33e129777827f3a9d20da00d6
2019-07-26 19:53:28 +00:00

47 lines
1.5 KiB
C#

using System.Runtime.CompilerServices;
namespace System.Collections.Generic
{
partial class Comparer<T>
{
static volatile Comparer<T> defaultComparer;
public static Comparer<T> Default {
get {
Comparer<T> comparer = defaultComparer;
if (comparer == null) {
comparer = CreateComparer();
defaultComparer = comparer;
}
return comparer;
}
}
static Comparer<T> CreateComparer() {
RuntimeType t = (RuntimeType)typeof(T);
if (typeof(IComparable<T>).IsAssignableFrom(t))
return (Comparer<T>)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(GenericComparer<>), t);
// If T is a Nullable<U> where U implements IComparable<U> return a NullableComparer<U>
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) {
RuntimeType u = (RuntimeType)t.GetGenericArguments()[0];
if (typeof(IComparable<>).MakeGenericType (u).IsAssignableFrom (u))
return (Comparer<T>)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(NullableComparer<>), u);
}
if (t.IsEnum)
return (Comparer<T>)RuntimeType.CreateInstanceForAnotherGenericParameter (typeof(EnumComparer<>), t);
// Otherwise return an ObjectComparer<T>
return new ObjectComparer<T> ();
}
}
partial class EnumComparer<T>
{
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public override int Compare (T x, T y) => JitHelpers.EnumCompareTo (x, y);
}
}