-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignatureExtensions.cs
47 lines (41 loc) · 1.41 KB
/
SignatureExtensions.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
using Cecil.AspectN.Matchers;
using Mono.Cecil;
using System.Collections.Generic;
namespace Cecil.AspectN
{
public static class SignatureExtensions
{
private static readonly Dictionary<Pair, MethodDefinition?> _Cache = [];
public static void CacheClear()
{
_Cache.Clear();
}
public static MethodDefinition? FindMethod(this TypeSignature signature, IMatcher matcher, bool compositeAccessibility)
{
var pair = new Pair(signature, matcher);
if (!_Cache.TryGetValue(pair, out var def))
{
if (signature.Reference is not TypeDefinition typeDef)
{
typeDef = signature.Reference.Resolve();
}
foreach (var methodDef in typeDef.Methods)
{
var methodSignature = SignatureParser.ParseMethod(methodDef, compositeAccessibility);
if (matcher.IsMatch(methodSignature))
{
def = methodDef;
break;
}
}
_Cache.Add(pair, def);
}
return def;
}
readonly struct Pair(TypeSignature signature, IMatcher matcher)
{
public readonly TypeSignature Signature = signature;
public readonly IMatcher Matcher = matcher;
}
}
}