-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathMemberComparer.cs
More file actions
43 lines (35 loc) · 1.16 KB
/
MemberComparer.cs
File metadata and controls
43 lines (35 loc) · 1.16 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Collections.Generic;
using System.Reflection;
namespace Microsoft.ClearScript.Util
{
internal sealed class MemberComparer<T> : EqualityComparer<T> where T : MemberInfo
{
public static readonly MemberComparer<T> Instance = new();
private MemberComparer()
{
}
public override bool Equals(T x, T y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
{
return false;
}
return MiscHelpers.Try(out var result, static ctx => UnsafeEquals(ctx.x, ctx.y), (x, y)) && result;
}
public override int GetHashCode(T obj)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
return ReferenceEquals(obj, null) ? 0 : obj.GetHashCode();
}
private static bool UnsafeEquals(T x, T y)
{
return (x.Module == y.Module) && (x.MetadataToken == y.MetadataToken);
}
}
}