-
Notifications
You must be signed in to change notification settings - Fork 1
/
Disassembler.cs
68 lines (63 loc) · 2.17 KB
/
Disassembler.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
using System.Reflection.Metadata;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Disassembler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
namespace Cilurbo;
static class Disassembler {
static public string Disassemble (this PEFile file)
{
using StringWriter writer = new ();
PlainTextOutput output = new (writer);
ReflectionDisassembler rd = new (output, CancellationToken.None);
rd.WriteAssemblyHeader (file);
output.WriteLine ();
rd.WriteModuleHeader (file);
return writer.ToString ();
}
static public string Disassemble (this IEntity entity)
{
using StringWriter writer = new ();
PlainTextOutput output = new (writer);
ReflectionDisassembler rd = new (output, CancellationToken.None);
var pe = entity.ParentModule.PEFile!;
switch (entity) {
case ITypeDefinition type:
rd.DisassembleType (pe, (TypeDefinitionHandle) type.MetadataToken);
break;
case IField field:
rd.DisassembleField (pe, (FieldDefinitionHandle) field.MetadataToken);
break;
case IProperty property:
rd.DisassembleProperty (pe, (PropertyDefinitionHandle) property.MetadataToken);
if (property.Getter is not null) {
output.WriteLine ();
rd.DisassembleMethod (pe, (MethodDefinitionHandle) property.Getter.MetadataToken);
}
if (property.Setter is not null) {
output.WriteLine ();
rd.DisassembleMethod (pe, (MethodDefinitionHandle) property.Setter.MetadataToken);
}
break;
case IEvent @event:
rd.DisassembleEvent (pe, (EventDefinitionHandle) @event.MetadataToken);
if (@event.AddAccessor is not null) {
output.WriteLine ();
rd.DisassembleMethod (pe, (MethodDefinitionHandle) @event.AddAccessor.MetadataToken);
}
if (@event.RemoveAccessor is not null) {
output.WriteLine ();
rd.DisassembleMethod (pe, (MethodDefinitionHandle) @event.RemoveAccessor.MetadataToken);
}
if (@event.InvokeAccessor is not null) {
output.WriteLine ();
rd.DisassembleMethod (pe, (MethodDefinitionHandle) @event.InvokeAccessor.MetadataToken);
}
break;
case IMethod method:
rd.DisassembleMethod (pe, (MethodDefinitionHandle) method.MetadataToken);
break;
}
return writer.ToString ();
}
}