This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathRouteValueEqualityComparer.cs
55 lines (50 loc) · 1.96 KB
/
RouteValueEqualityComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Microsoft.AspNetCore.Routing
{
/// <summary>
/// An <see cref="IEqualityComparer{Object}"/> implementation that compares objects as-if
/// they were route value strings.
/// </summary>
/// <remarks>
/// Values that are are not strings are converted to strings using
/// <c>Convert.ToString(x, CultureInfo.InvariantCulture)</c>. <c>null</c> values are converted
/// to the empty string.
///
/// strings are compared using <see cref="StringComparison.OrdinalIgnoreCase"/>.
/// </remarks>
public class RouteValueEqualityComparer : IEqualityComparer<object>
{
public static readonly RouteValueEqualityComparer Default = new RouteValueEqualityComparer();
/// <inheritdoc />
public new bool Equals(object x, object y)
{
var stringX = x as string ?? Convert.ToString(x, CultureInfo.InvariantCulture);
var stringY = y as string ?? Convert.ToString(y, CultureInfo.InvariantCulture);
if (string.IsNullOrEmpty(stringX) && string.IsNullOrEmpty(stringY))
{
return true;
}
else
{
return string.Equals(stringX, stringY, StringComparison.OrdinalIgnoreCase);
}
}
/// <inheritdoc />
public int GetHashCode(object obj)
{
var stringObj = obj as string ?? Convert.ToString(obj, CultureInfo.InvariantCulture);
if (string.IsNullOrEmpty(stringObj))
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(string.Empty);
}
else
{
return StringComparer.OrdinalIgnoreCase.GetHashCode(stringObj);
}
}
}
}