-
Notifications
You must be signed in to change notification settings - Fork 2
/
ILGeneration.cs
165 lines (106 loc) · 5.68 KB
/
ILGeneration.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ilgen_convert {
public static class ILGeneration {
public static void CreateDynamicMethod(this ILProcessor processor, MethodDefinition method) {
// determine the name of dynamic method (param #1)
processor.Emit(OpCodes.Ldstr, Guid.NewGuid().ToString());
// determine the method attributes (param #2)
// MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.Static;
processor.Emit(OpCodes.Ldc_I4, 0x16);
// determine the method calling convention (param #3)
// CallingConventions.Standard
processor.Emit(OpCodes.Ldc_I4_1);
// determine the return type of the method (param #4)
processor.EmitType(method.ReturnType);
// create a new array to hold parameter types (param #5)
processor.EmitTypeArray(method.Parameters);
// determine the type of the method owner (param #6)
processor.EmitType(method.DeclaringType);
// determine the skipVisiblity parameter value
processor.Emit(OpCodes.Ldc_I4_0);
// create an instance of DynamicMethod with the parameters on the stack (string, Type, Type[])
processor.Emit(OpCodes.Newobj, Program.MethodReferences["DynamicMethodConstructor"]);
}
/// new Type[] { typeof(string), typeof(System.Reflection.BindingFlags), typeof(System.Reflection.Binder), typeof(Type[]), typeof(System.Reflection.ParameterModifier[]) }
public static void EmitMethodGetter(this ILProcessor processor, MethodDefinition method) {
// get the parent type and store it on the stack
processor.EmitType(method.DeclaringType);
if (method.IsConstructor) {
processor.EmitTypeArray(method.Parameters);
processor.Emit(OpCodes.Callvirt, Program.MethodReferences["GetConstructorInfoTypes"]);
return;
}
// make sure the method is public
method.IsPrivate = false;
method.IsPublic = true;
// the method name (param #1)
processor.Emit(OpCodes.Ldstr, method.Name);
// the binding flags (param #2)
processor.Emit(OpCodes.Ldc_I4, Utils.GetBindingFlags(method));
// the binder (param #3)
processor.Emit(OpCodes.Ldnull);
// the parameter types (param #4)
processor.EmitTypeArray(method.Parameters);
// the parameter modifiers (param $5)
processor.Emit(OpCodes.Ldnull);
// call GetMethodInfo function, leaving the returned value on the eval stack
processor.Emit(OpCodes.Callvirt, Program.MethodReferences["GetMethodInfo"]);
}
public static void EmitMethodGetter(this ILProcessor processor, MethodReference method) {
processor.EmitType(method.DeclaringType);
if (method.Name == ".ctor") {
processor.EmitTypeArray(method.Parameters);
processor.Emit(OpCodes.Call, Program.MethodReferences["GetConstructorInfoTypes"]);
return;
}
processor.Emit(OpCodes.Ldstr, method.Name);
processor.EmitTypeArray(method.Parameters);
processor.Emit(OpCodes.Call, Program.MethodReferences["GetMethodInfoTypes"]);
}
public static void EmitFieldGetter(this ILProcessor processor, FieldDefinition field) {
// make sure the field is public
field.IsPrivate = false;
field.IsPublic = true;
// get the parent type and store it on the stack
processor.EmitType(field.DeclaringType);
// store the parameter name on the stack (param #1)
processor.Emit(OpCodes.Ldstr, field.Name);
// store the binding fields on the stack (param #2)
processor.Emit(OpCodes.Ldc_I4, Utils.GetBindingFlags(field));
// call the function to store FieldInfo object on the stack (string, BindingFlags)
processor.Emit(OpCodes.Callvirt, Program.MethodReferences["GetFieldInfo"]);
}
public static void EmitTypeArray(this ILProcessor processor, Collection<ParameterDefinition> parameters) {
// determine length of array
processor.Emit(OpCodes.Ldc_I4_S, Convert.ToSByte(parameters.Count));
// create the array
processor.Emit(OpCodes.Newarr, Program.TypeReferences["Type"]);
// iterate through parameters in collection
for (int pI = 0; pI < parameters.Count; pI++) {
// load array object onto eval stack
processor.Emit(OpCodes.Dup);
// load the index onto the stack
processor.Emit(OpCodes.Ldc_I4, pI);
// load the type onto the stack
processor.EmitType(parameters[pI].ParameterType);
// move the 'type' into array and index pI
processor.Emit(OpCodes.Stelem_Ref);
}
}
public static void EmitType(this ILProcessor processor, TypeReference reference) {
processor.Emit(OpCodes.Ldtoken, reference);
processor.Emit(OpCodes.Call, Program.MethodReferences["GetTypeFromHandle"]);
}
public static void EmitMarkLabel(this ILProcessor processor, VariableDefinition label) {
processor.Emit(OpCodes.Ldloc, label);
processor.Emit(OpCodes.Callvirt, Program.MethodReferences["MarkLabel"]);
}
}
}