forked from Fody/MethodTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceFinder.cs
74 lines (64 loc) · 3.43 KB
/
ReferenceFinder.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Fody;
using Mono.Cecil;
public partial class ModuleWeaver
{
public MethodReference TraceWriteLineMethod;
public MethodReference StartNewMethod;
public MethodReference StopMethod;
public TypeReference StopwatchType;
public MethodReference StringFormatWithArray;
public MethodReference ConcatMethod;
public MethodReference IsRunning;
public MethodReference Elapsed;
public MethodReference ElapsedMilliseconds;
public MethodReference GetMethodFromHandle;
public MethodReference ObjectConstructorMethod;
public MethodReference MaxMethod;
public MethodReference GetTicksMethod;
public MethodReference UtcNowMethod;
public TypeReference DateTimeType;
public MethodReference Int64ToString;
public void FindReferences()
{
if (!TryFindTypeDefinition("System.Diagnostics.Trace", out var traceType))
{
if (!TryFindTypeDefinition("System.Diagnostics.Debug", out traceType))
{
throw new WeavingException("Could not find either Trace Or Debug");
}
}
var writeLine = traceType.Method("WriteLine", "String");
TraceWriteLineMethod = ModuleDefinition.ImportReference(writeLine);
var objectConstructor = TypeSystem.ObjectDefinition.Method(".ctor");
ObjectConstructorMethod = ModuleDefinition.ImportReference(objectConstructor);
var mathType = FindTypeDefinition("System.Math");
MaxMethod = ModuleDefinition.ImportReference(mathType.Method("Max", "Int64", "Int64"));
var dateTimeType = FindTypeDefinition("System.DateTime");
DateTimeType = ModuleDefinition.ImportReference(dateTimeType);
UtcNowMethod = ModuleDefinition.ImportReference(dateTimeType.Method("get_UtcNow"));
GetTicksMethod = ModuleDefinition.ImportReference(dateTimeType.Method("get_Ticks"));
var methodBaseType = FindTypeDefinition("System.Reflection.MethodBase");
var methodBase = methodBaseType.Method("GetMethodFromHandle", "RuntimeMethodHandle", "RuntimeTypeHandle");
GetMethodFromHandle = ModuleDefinition.ImportReference(methodBase);
var int64ToString = TypeSystem.Int64Definition.Method("ToString");
Int64ToString = ModuleDefinition.ImportReference(int64ToString);
var formatMethod = TypeSystem.StringDefinition.Method("Format", "String", "Object[]");
StringFormatWithArray = ModuleDefinition.ImportReference(formatMethod);
var concatMethod = TypeSystem.StringDefinition.Method("Concat", "String", "String", "String");
ConcatMethod = ModuleDefinition.ImportReference(concatMethod);
if (TryFindTypeDefinition("System.Diagnostics.Stopwatch", out var stopwatchType))
{
StopwatchType = ModuleDefinition.ImportReference(stopwatchType);
StartNewMethod = ModuleDefinition.ImportReference(stopwatchType.Method("StartNew"));
StopMethod = ModuleDefinition.ImportReference(stopwatchType.Method("Stop"));
Elapsed = ModuleDefinition.ImportReference(stopwatchType.Method("get_Elapsed"));
ElapsedMilliseconds = ModuleDefinition.ImportReference(stopwatchType.Method("get_ElapsedMilliseconds"));
IsRunning = ModuleDefinition.ImportReference(stopwatchType.Method("get_IsRunning"));
}
else
{
// Note: injected stopwatch is not supported for TimeSpan elapsed, should we error or add?
InjectStopwatchType();
}
}
}