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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
#if !READ_ONLY
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using SR = System.Reflection;
using System.Runtime.CompilerServices;
using Mono.Cecil.Cil;
using NUnit.Framework;
namespace Mono.Cecil.Tests {
[TestFixture]
public class ImportReflectionTests : BaseTestFixture {
[Test]
public void ImportString ()
{
var get_string = Compile<Func<string>> ((_, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldstr, "yo dawg!");
il.Emit (OpCodes.Ret);
});
Assert.AreEqual ("yo dawg!", get_string ());
}
[Test]
public void ImportInt ()
{
var add = Compile<Func<int, int, int>> ((_, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Add);
il.Emit (OpCodes.Ret);
});
Assert.AreEqual (42, add (40, 2));
}
[Test]
public void ImportStringByRef ()
{
var get_string = Compile<Func<string, string>> ((module, body) => {
var type = module.Types [1];
var method_by_ref = new MethodDefinition {
Name = "ModifyString",
IsPrivate = true,
IsStatic = true,
};
type.Methods.Add (method_by_ref);
method_by_ref.MethodReturnType.ReturnType = module.ImportReference (typeof (void));
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (typeof (string))));
method_by_ref.Parameters.Add (new ParameterDefinition (module.ImportReference (typeof (string).MakeByRefType ())));
var m_il = method_by_ref.Body.GetILProcessor ();
m_il.Emit (OpCodes.Ldarg_1);
m_il.Emit (OpCodes.Ldarg_0);
m_il.Emit (OpCodes.Stind_Ref);
m_il.Emit (OpCodes.Ret);
var v_0 = new VariableDefinition (module.ImportReference (typeof (string)));
body.Variables.Add (v_0);
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Stloc, v_0);
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldloca, v_0);
il.Emit (OpCodes.Call, method_by_ref);
il.Emit (OpCodes.Ldloc_0);
il.Emit (OpCodes.Ret);
});
Assert.AreEqual ("foo", get_string ("foo"));
}
[Test]
public void ImportStringArray ()
{
var identity = Compile<Func<string [,], string [,]>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ret);
});
var array = new string [2, 2];
Assert.AreEqual (array, identity (array));
}
[Test]
public void ImportFieldStringEmpty ()
{
var get_empty = Compile<Func<string>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldsfld, module.ImportReference (typeof (string).GetField ("Empty")));
il.Emit (OpCodes.Ret);
});
Assert.AreEqual ("", get_empty ());
}
[Test]
public void ImportStringConcat ()
{
var concat = Compile<Func<string, string, string>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Call, module.ImportReference (typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) })));
il.Emit (OpCodes.Ret);
});
Assert.AreEqual ("FooBar", concat ("Foo", "Bar"));
}
[Test]
public void GeneratedAssemblyCulture ()
{
var id = Compile<Func<int, int>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ret);
});
Assert.AreEqual ("", id.Method.DeclaringType.Assembly.GetName ().CultureInfo.Name);
}
public class Generic<T> {
public T Field;
public T Method (T t)
{
return t;
}
public TS GenericMethod<TS> (T t, TS s)
{
return s;
}
public Generic<TS> ComplexGenericMethod<TS> (T t, TS s)
{
return new Generic<TS> { Field = s };
}
}
[Test]
public void ImportGenericField ()
{
var get_field = Compile<Func<Generic<string>, string>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldfld, module.ImportReference (typeof (Generic<string>).GetField ("Field")));
il.Emit (OpCodes.Ret);
});
var generic = new Generic<string> {
Field = "foo",
};
Assert.AreEqual ("foo", get_field (generic));
}
[Test]
public void ImportGenericMethod ()
{
var generic_identity = Compile<Func<Generic<int>, int, int>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<int>).GetMethod ("Method")));
il.Emit (OpCodes.Ret);
});
Assert.AreEqual (42, generic_identity (new Generic<int> (), 42));
}
[Test]
public void ImportGenericMethodSpec ()
{
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<string>).GetMethod ("GenericMethod").MakeGenericMethod (typeof (int))));
il.Emit (OpCodes.Ret);
});
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
}
[Test]
public void ImportComplexGenericMethodSpec ()
{
var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
var il = body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldnull);
il.Emit (OpCodes.Ldarg_1);
il.Emit (OpCodes.Callvirt, module.ImportReference (typeof (Generic<string>).GetMethod ("ComplexGenericMethod").MakeGenericMethod (typeof (int))));
il.Emit (OpCodes.Ldfld, module.ImportReference (typeof (Generic<int>).GetField ("Field")));
il.Emit (OpCodes.Ret);
});
Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
}
public class Foo<TFoo> {
public List<TFoo> list;
}
[Test]
public void ImportGenericTypeDefOrOpen ()
{
using (var module = typeof (Foo<>).ToDefinition ().Module) {
var foo_def = module.ImportReference (typeof (Foo<>));
var foo_open = module.ImportReference (typeof (Foo<>), foo_def);
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName);
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName);
}
}
[Test]
public void ImportGenericTypeFromContext ()
{
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
var foo_def = typeof (Foo<>).ToDefinition ();
using (var module = foo_def.Module) {
var generic_foo = module.ImportReference (generic_list_foo_open, foo_def);
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>",
generic_foo.FullName);
}
}
[Test]
public void ImportGenericTypeDefFromContext ()
{
var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]);
var generic_foo_open = typeof (Generic<>).MakeGenericType (foo_open);
var foo_def = typeof (Foo<>).ToDefinition ();
using (var module = foo_def.Module) {
var generic_foo = module.ImportReference (generic_foo_open, foo_def);
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>",
generic_foo.FullName);
}
}
[Test]
public void ImportArrayTypeDefFromContext ()
{
var foo_open = typeof (Foo<>).MakeGenericType (typeof (Foo<>).GetGenericArguments () [0]);
var foo_open_array = foo_open.MakeArrayType ();
var foo_def = typeof (Foo<>).ToDefinition ();
using (var module = foo_def.Module) {
var array_foo = module.ImportReference (foo_open_array, foo_def);
Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]",
array_foo.FullName);
}
}
[Test]
public void ImportGenericFieldFromContext ()
{
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
var generic_list_foo_open_field = generic_list_foo_open.GetField ("Field");
var foo_def = typeof (Foo<>).ToDefinition ();
using (var module = foo_def.Module) {
var generic_field = module.ImportReference (generic_list_foo_open_field, foo_def);
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field",
generic_field.FullName);
}
}
[Test]
public void ImportGenericMethodFromContext ()
{
var list_foo = typeof (Foo<>).GetField ("list").FieldType;
var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo);
var generic_list_foo_open_method = generic_list_foo_open.GetMethod ("Method");
var foo_def = typeof (Foo<>).ToDefinition ();
using (var module = foo_def.Module) {
var generic_method = module.ImportReference (generic_list_foo_open_method, foo_def);
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(T)",
generic_method.FullName);
}
}
[Test]
public void ImportMethodOnOpenGenericType ()
{
using (var module = typeof (Generic<>).ToDefinition ().Module) {
var method = module.ImportReference (typeof (Generic<>).GetMethod ("Method"));
Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName);
}
}
[Test]
public void ImportGenericMethodOnOpenGenericType ()
{
using (var module = typeof (Generic<>).ToDefinition ().Module) {
var generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"));
Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName);
generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method);
Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName);
}
}
delegate void Emitter (ModuleDefinition module, MethodBody body);
[MethodImpl (MethodImplOptions.NoInlining)]
static TDelegate Compile<TDelegate> (Emitter emitter)
where TDelegate : class
{
var name = GetTestCaseName ();
var module = CreateTestModule<TDelegate> (name, emitter);
var assembly = LoadTestModule (module);
return CreateRunDelegate<TDelegate> (GetTestCase (name, assembly));
}
static TDelegate CreateRunDelegate<TDelegate> (Type type)
where TDelegate : class
{
return (TDelegate) (object) Delegate.CreateDelegate (typeof (TDelegate), type.GetMethod ("Run"));
}
static Type GetTestCase (string name, SR.Assembly assembly)
{
return assembly.GetType (name);
}
static SR.Assembly LoadTestModule (ModuleDefinition module)
{
using (var stream = new MemoryStream ()) {
module.Write (stream);
File.WriteAllBytes (Path.Combine (Path.Combine (Path.GetTempPath (), "cecil"), module.Name + ".dll"), stream.ToArray ());
return SR.Assembly.Load (stream.ToArray ());
}
}
static ModuleDefinition CreateTestModule<TDelegate> (string name, Emitter emitter)
{
var module = CreateModule (name);
var type = new TypeDefinition (
"",
name,
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract,
module.ImportReference (typeof (object)));
module.Types.Add (type);
var method = CreateMethod (type, typeof (TDelegate).GetMethod ("Invoke"));
emitter (module, method.Body);
return module;
}
static MethodDefinition CreateMethod (TypeDefinition type, SR.MethodInfo pattern)
{
var module = type.Module;
var method = new MethodDefinition {
Name = "Run",
IsPublic = true,
IsStatic = true,
};
type.Methods.Add (method);
method.MethodReturnType.ReturnType = module.ImportReference (pattern.ReturnType);
foreach (var parameter_pattern in pattern.GetParameters ())
method.Parameters.Add (new ParameterDefinition (module.ImportReference (parameter_pattern.ParameterType)));
return method;
}
static ModuleDefinition CreateModule (string name)
{
return ModuleDefinition.CreateModule (name, ModuleKind.Dll);
}
[MethodImpl (MethodImplOptions.NoInlining)]
static string GetTestCaseName ()
{
var stack_trace = new StackTrace ();
var stack_frame = stack_trace.GetFrame (2);
return "ImportReflection_" + stack_frame.GetMethod ().Name;
}
}
}
#endif
|