-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodSignature.cs
More file actions
90 lines (78 loc) · 2.95 KB
/
MethodSignature.cs
File metadata and controls
90 lines (78 loc) · 2.95 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Microsoft.CodeAnalysis;
namespace TedToolkit.RoslynHelper;
/// <summary>
/// The signature of a method
/// </summary>
/// <param name="methodSymbol"></param>
public readonly struct MethodSignature(IMethodSymbol methodSymbol) : IEquatable<MethodSignature>
{
/// <summary>
/// Should check the equality with containing type.
/// </summary>
public static bool EqualityWithContainingType { get; set; } = true;
/// <summary>
/// the name of the method.
/// </summary>
public string MethodName { get; } = methodSymbol.Name;
/// <summary>
/// The containing type.
/// </summary>
public ITypeSymbol ContainingType { get; } = methodSymbol.IsExtensionMethod
? methodSymbol.Parameters[0].Type.OriginalDefinition
: methodSymbol.ContainingType.OriginalDefinition;
/// <summary>
/// Tye parameter types.
/// </summary>
public ITypeSymbol[] ParameterTypes { get; } = methodSymbol.Parameters
.Skip(methodSymbol.IsExtensionMethod ? 1 : 0)
.Select(p => p.Type.OriginalDefinition)
.ToArray();
/// <summary>
/// The Ref Kinds.
/// </summary>
public RefKind[] RefKinds { get; } = methodSymbol.Parameters
.Skip(methodSymbol.IsExtensionMethod ? 1 : 0)
.Select(i => i.RefKind)
.ToArray();
/// <summary>
/// The type Argument counts.
/// </summary>
public int TypeArgumentsCount { get; } =
methodSymbol.TypeArguments.Length + methodSymbol.ContainingType.TypeArguments.Length;
/// <inheritdoc />
public bool Equals(MethodSignature other)
{
if (!MethodName.Equals(other.MethodName)) return false;
if (!TypeArgumentsCount.Equals(other.TypeArgumentsCount)) return false;
if (EqualityWithContainingType &&
!ContainingType.Equals(other.ContainingType, SymbolEqualityComparer.Default)) return false;
if (!ParameterTypes.Length.Equals(other.ParameterTypes.Length)) return false;
for (var i = 0; i < ParameterTypes.Length; i++)
{
var thisType = ParameterTypes[i];
var otherType = other.ParameterTypes[i];
if (thisType.TypeKind == TypeKind.TypeParameter
&& otherType.TypeKind == TypeKind.TypeParameter)
continue;
if (RefKinds[i] != other.RefKinds[i]) return false;
if (!thisType.Equals(otherType, SymbolEqualityComparer.Default)) return false;
}
return true;
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
return obj is MethodSignature other && Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
unchecked
{
var hashCode = MethodName.GetHashCode();
hashCode = (hashCode * 397) ^ ParameterTypes.Length.GetHashCode();
hashCode = (hashCode * 397) ^ TypeArgumentsCount.GetHashCode();
return hashCode;
}
}
}