File: ILGeneratorTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (615 lines) | stat: -rw-r--r-- 17,596 bytes parent folder | download | duplicates (6)
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//
// ILGeneratorTest.cs - NUnit Test Cases for the ILGenerator class
//
// Marek Safar (marek.safar@seznam.cz)
//
// (C) Novell, Inc.  http://www.novell.com

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Threading;

using NUnit.Framework;

namespace MonoTests.System.Reflection.Emit
{
	[TestFixture]
	public class ILGeneratorTest
	{
		ModuleBuilder modulebuilder;
		TypeBuilder tb;
		ILGenerator il_gen;

		void DefineBasicMethod ()
		{
			MethodBuilder mb = tb.DefineMethod("F",
				MethodAttributes.Public, typeof(string), null);
			il_gen = mb.GetILGenerator ();
		}

		[SetUp]
		public void SetUp ()
		{
			AssemblyName assemblyName = new AssemblyName ();
			assemblyName.Name = "MonoTests.System.Reflection.Emit.ILGeneratorTest";

			AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
				assemblyName, AssemblyBuilderAccess.Run);

			modulebuilder = assembly.DefineDynamicModule ("module1");
			tb = modulebuilder.DefineType ("T", TypeAttributes.Public);
		}

		[Test]
		public void DeclareLocal_LocalType_Null ()
		{
			DefineBasicMethod ();

			try {
				il_gen.DeclareLocal (null);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("localType", ex.ParamName, "#A");
			}

			try {
				il_gen.DeclareLocal (null, false);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("localType", ex.ParamName, "#B6");
			}
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void DefineFilterBodyWithTypeNotNull ()
		{
			DefineBasicMethod ();
			il_gen.BeginExceptionBlock ();
			il_gen.EmitWriteLine ("in try");
			il_gen.BeginExceptFilterBlock ();
			il_gen.EmitWriteLine ("in filter head");
			il_gen.BeginCatchBlock (typeof (Exception));
			il_gen.EmitWriteLine ("in filter body");
			il_gen.EndExceptionBlock ();
		}

		[Test] // bug #81431
		public void FilterAndCatchBlock ()
		{
			DefineBasicMethod ();
			ILGenerator il = il_gen;
			il.BeginExceptionBlock ();
			il.BeginExceptFilterBlock ();
			il.BeginCatchBlock (null);
			il.BeginCatchBlock (typeof (SystemException));
		}
		
		[Test]
		[ExpectedException (typeof (InvalidOperationException))]
		public void InvalidFilterBlock1 ()
		{
			DefineBasicMethod ();
			ILGenerator il = il_gen;
			il.BeginExceptionBlock ();
			il.BeginExceptFilterBlock ();
			il.EndExceptionBlock ();
		}
		
		[Test]
		public void ValidFilterBlock1 ()
		{
			DefineBasicMethod ();
			ILGenerator il = il_gen;
			il.BeginExceptionBlock ();
			il.BeginExceptFilterBlock ();
			il.BeginFaultBlock ();
			il.EndExceptionBlock ();
		}
		
		[Test]
		public void ValidFilterBlock2 ()
		{
			DefineBasicMethod ();
			ILGenerator il = il_gen;
			il.BeginExceptionBlock ();
			il.BeginExceptFilterBlock ();
			il.BeginFinallyBlock ();
			il.EndExceptionBlock ();
		}
		
		/// <summary>
		/// Try to emit something like that:
		///
		/// .method public static bool TestFilter (bool execute_handler)
		/// {
		/// 	.locals init(bool)
		/// 	try {
		/// 		newobj  instance void [mscorlib]System.Exception::.ctor()
		/// 		throw
		/// 	} filter {
		/// 		pop
		/// 		ldarg.0
		/// 		endfilter
		/// 	} {
		/// 		ldc.i4.1
		/// 		stloc.0
		/// 		leave quit
		/// 	}
		/// 	ldc.i4.0
		/// 	stloc.0
		/// quit:
		/// 	ldloc.0
		/// 	ret
		/// }
		///
		/// It should return true if the handler has been executed
		/// Otherwise, the exception should not be catched
		/// </summary>
		void DefineTestFilterMethod ()
		{
			MethodBuilder mb = tb.DefineMethod("TestFilter",
				MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type [] { typeof (bool) });

			ConstructorInfo exCtor = typeof (Exception).GetConstructor (new Type [0]);

			il_gen = mb.GetILGenerator ();
			il_gen.DeclareLocal (typeof (bool));
			Label quit = il_gen.DefineLabel ();
			il_gen.BeginExceptionBlock ();
			il_gen.Emit (OpCodes.Newobj, exCtor);
			il_gen.Emit (OpCodes.Throw);
			il_gen.BeginExceptFilterBlock ();
			il_gen.Emit (OpCodes.Pop);
			il_gen.Emit (OpCodes.Ldarg_0);
			il_gen.BeginCatchBlock (null);
			il_gen.Emit (OpCodes.Ldc_I4_1);
			il_gen.Emit (OpCodes.Stloc_0);
			il_gen.Emit (OpCodes.Leave, quit);
			il_gen.EndExceptionBlock ();
			il_gen.Emit (OpCodes.Ldc_I4_0);
			il_gen.Emit (OpCodes.Stloc_0);
			il_gen.MarkLabel (quit);
			il_gen.Emit (OpCodes.Ldloc_0);
			il_gen.Emit (OpCodes.Ret);
		}

		[Test] // Emit (OpCode, ConstructorInfo)
		[Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304610
		public void Emit3_Constructor_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Newobj, (ConstructorInfo) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
			}
		}

		[Test] // Emit (OpCode, ConstructorInfo)
		[Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304610
		public void Emit3_Constructor_Null_MS ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Newobj, (ConstructorInfo) null);
				Assert.Fail ("#1");
			} catch (NullReferenceException) {
			}
		}

		[Test] // Emit (OpCode, FieldInfo)
		public void Emit5_Field_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Ldsfld, (FieldInfo) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
			}
		}

		[Test] // Emit (OpCode, Label [])
		[Category ("NotDotNet")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304610
		public void Emit10_Labels_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (Label []) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("labels", ex.ParamName, "#6");
			}
		}

		[Test]
		[Category ("NotWorking")] // MS bug: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304610
		public void Emit10_Labels_Null_MS ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (Label []) null);
				Assert.Fail ("#1");
			} catch (NullReferenceException) {
			}
		}

		[Test] // Emit (OpCode, LocalBuilder)
		public void Emit11_Local_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (LocalBuilder) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("local", ex.ParamName, "#6");
			}
		}

		[Test] // Emit (OpCode, MethodInfo)
		public void Emit12_Method_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (MethodInfo) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("meth", ex.ParamName, "#6");
			}
		}

		[Test] // Emit (OpCode, SignatureHelper)
		public void Emit14_Signature_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (SignatureHelper) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
			}
		}

		[Test] // Emit (OpCode, String)
		public void Emit16_String_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (String) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
			}
		}

		[Test] // Emit (OpCode, Type)
		public void Emit16_Type_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.Emit (OpCodes.Switch, (Type) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
			}
		}

		[Test]
		public void EmitCall_MethodInfo_Null ()
		{
			DefineBasicMethod ();
			try {
				il_gen.EmitCall (OpCodes.Call, (MethodInfo) null, null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.IsNotNull (ex.ParamName, "#5");
				Assert.AreEqual ("methodInfo", ex.ParamName, "#6");
			}
		}

		[Test]
		public void TestFilterEmittingWithHandlerExecution ()
		{
			DefineTestFilterMethod ();
			Type dynt = tb.CreateType ();
			
			MethodInfo tf = dynt.GetMethod ("TestFilter");
			Assert.IsTrue ((bool) tf.Invoke (null, new object [] { true }));
		}

		delegate void FooFoo ();

		static void Foo ()
		{
		}

		[Test]
		public void TestEmitCalliWithNullReturnType ()
		{
			MethodBuilder mb = tb.DefineMethod ("F",
				MethodAttributes.Public | MethodAttributes.Static, null, new Type [] { typeof (IntPtr) });
			mb.SetImplementationFlags (MethodImplAttributes.NoInlining);
			il_gen = mb.GetILGenerator ();
			il_gen.Emit (OpCodes.Ldarg_0);
			il_gen.EmitCalli (OpCodes.Calli, CallingConvention.StdCall, null, Type.EmptyTypes);
			il_gen.Emit (OpCodes.Ret);
	
			Type dynt = tb.CreateType ();
			dynt.GetMethod ("F", BindingFlags.Public | BindingFlags.Static).Invoke (
				null, new object [] { Marshal.GetFunctionPointerForDelegate (new FooFoo (Foo)) });
		}

		//Test for #509131
		[Test]
		public void TestEmitCallIgnoresOptionalArgsForNonVarargMethod ()
		{
			DefineBasicMethod ();
			try {
				il_gen.EmitCall (OpCodes.Call, typeof (object).GetMethod ("GetHashCode"), new Type[] { typeof (string) });
			} catch (InvalidOperationException ex) {
				Assert.Fail ("#1");
			}
		}

		[Test]
		[ExpectedException (typeof (Exception))]
		public void TestFilterEmittingWithoutHandlerExecution ()
		{
			DefineTestFilterMethod ();
			Type dynt = tb.CreateType ();
			
			MethodInfo tf = dynt.GetMethod ("TestFilter");
			try {
				tf.Invoke (null, new object [] { false });
			} catch (TargetInvocationException tie) {
				throw tie.InnerException;
			}
		}

		[Test]
		public void TestEmitLocalInfoWithNopOpCode ()
		{
			var method_builder = tb.DefineMethod ("linop", MethodAttributes.Public | MethodAttributes.Static, typeof (bool), Type.EmptyTypes);
			il_gen = method_builder.GetILGenerator ();

			var local = il_gen.DeclareLocal (typeof (int));
			il_gen.Emit (OpCodes.Nop, local);
			il_gen.Emit (OpCodes.Ldc_I4_1);
			il_gen.Emit (OpCodes.Ret);

			var type = tb.CreateType ();
			var method = type.GetMethod ("linop");

			Assert.IsNotNull (method);
			Assert.IsTrue ((bool) method.Invoke (null, new object [0]));
		}

		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void LdObjByRef () {
			DefineBasicMethod ();
			ILGenerator ig = il_gen;

			ig.Emit (OpCodes.Ldtoken, typeof (int).MakeByRefType ());
		}



		[Test] //bug #649017
		public void GtdEncodingAsOpenInstance () {
	        AssemblyName asmname = new AssemblyName ();
	        asmname.Name = "test";
	        AssemblyBuilder asmbuild = Thread.GetDomain ().DefineDynamicAssembly (asmname, AssemblyBuilderAccess.RunAndSave);
	        ModuleBuilder modbuild = asmbuild.DefineDynamicModule ("modulename", "test.exe");
	
	        TypeBuilder myType = modbuild.DefineType ("Sample", TypeAttributes.Public);
	
	        string[] typeParamNames = { "TFirst" };
	        myType.DefineGenericParameters (typeParamNames);
	
	        var nested = myType.DefineNestedType ("nested");
	        nested.DefineGenericParameters (typeParamNames);
	
	        var m = myType.DefineMethod ("test", MethodAttributes.Public);
	        m.SetParameters (myType);
	
	        var ilgen = m.GetILGenerator ();
	        ilgen.Emit (OpCodes.Castclass, nested);
	        ilgen.Emit (OpCodes.Castclass, typeof (List<>));
	        ilgen.Emit (OpCodes.Ldtoken, nested);
	        ilgen.Emit (OpCodes.Ldtoken, typeof (List<>));
	
	        var baked = myType.CreateType ();
	        nested.CreateType ();
	
			var method = baked.GetMethod ("test");
			var body = method.GetMethodBody ();
			/*
			The resulting IL is:
			[ 0] 0x74 token:uint
			[ 5] 0x74 token:uint
			[10] 0xd0 token:uint
			[10] 0xd0 token:uint
			The first two tokens must be to typespecs and the last two to typeref/typedef*/
			var il = body.GetILAsByteArray ();
		
			Assert.AreEqual (20, il.Length, "#1");
			Assert.AreEqual (0x1B, il [4]); //typespec
			Assert.AreEqual (0x1B, il [9]); //typespec
			Assert.AreEqual (0x02, il [14]); //typedef
			Assert.AreEqual (0x01, il [19]); //typeref
		}

		[Test]
		public void MethodRefTokenSame () {
			// Get the same non-virtual method from a base and a derived type so
			// that the MemberInfo:DeclaredType differs but the tokens are the same.
			//
			// Regression test for bugzilla #59364

			DefineBasicMethod ();

			var m1 = typeof (object).GetMethod ("GetType");
			var m2 = typeof (string).GetMethod ("GetType");

			var value_getter = typeof (RuntimeMethodHandle).GetProperty ("Value").GetMethod;

			var il = il_gen;

			var loc = il.DeclareLocal (typeof (RuntimeMethodHandle));

			// return ((int)(RuntimeMethodHandle (m1).Value == RuntimeMethodHandle (m2).Value)).ToString ()
			il.Emit (OpCodes.Ldtoken, m1);
			il.Emit (OpCodes.Stloc, loc);
			il.Emit (OpCodes.Ldloca, loc);
			il.Emit (OpCodes.Call, value_getter);
			il.Emit (OpCodes.Ldtoken, m2);
			il.Emit (OpCodes.Stloc, loc);
			il.Emit (OpCodes.Ldloca, loc);
			il.Emit (OpCodes.Call, value_getter);
			il.Emit (OpCodes.Ceq);
			il.Emit (OpCodes.Box, typeof (Int32));
			il.Emit (OpCodes.Callvirt, typeof (object).GetMethod ("ToString"));
			il.Emit (OpCodes.Ret);

			var baked = tb.CreateType ();

			var x = Activator.CreateInstance (baked);
			var m = baked.GetMethod ("F");

			var s = m.Invoke (x, null);

			Assert.AreEqual ("1", s);
			
		}

		public class Base<T> {
			public int x;

			public void M () { }
		}

		public class Derived : Base<string> {
		}

		[Test]
		public void FieldRefTokenSame () {
			DefineBasicMethod ();

			// Get the same field from a base and a derived type so hat
			// the MemberInfo:DeclaredType differs but the tokens are the same.
			//
			// Regression test for bugzilla #59364

			var f1 = typeof (Base<string>).GetField ("x");
			var f2 = typeof (Derived).GetField ("x");

			var value_getter = typeof (RuntimeFieldHandle).GetProperty("Value").GetMethod;

			var il = il_gen;

			var loc = il.DeclareLocal (typeof (RuntimeFieldHandle));

			il.Emit (OpCodes.Ldtoken, f1);
			il.Emit (OpCodes.Stloc, loc);
			il.Emit (OpCodes.Ldloca, loc);
			il.Emit (OpCodes.Call, value_getter);
			il.Emit (OpCodes.Ldtoken, f2);
			il.Emit (OpCodes.Stloc, loc);
			il.Emit (OpCodes.Ldloca, loc);
			il.Emit (OpCodes.Call, value_getter);
			il.Emit (OpCodes.Ceq);
			il.Emit (OpCodes.Box, typeof (Int32));
			il.Emit (OpCodes.Callvirt, typeof (object).GetMethod ("ToString"));
			il.Emit (OpCodes.Ret);

			var baked = tb.CreateType ();

			var x = Activator.CreateInstance (baked);
			var m = baked.GetMethod ("F");

			var s = m.Invoke (x, null);

			Assert.AreEqual ("1", s);
		}

		[Test]
		public void MethodSpecTokenSame () {
			DefineBasicMethod ();
			// Get the same method from a base generic instance and
			// a type derived from it so that the
			// MemberInfo:DeclaredType differs but the tokens are
			// the same.
			//
			// Regression test for bugzilla #60233

			var m1 = typeof (Base<string>).GetMethod ("M");
			var m2 = typeof (Derived).GetMethod ("M");

			var il = il_gen;

			var loc = il.DeclareLocal (typeof (Derived));

			il.Emit (OpCodes.Newobj, typeof (Derived).GetConstructor(new Type[]{}));
			il.Emit (OpCodes.Stloc, loc);
			il.Emit (OpCodes.Ldloc, loc);
			il.Emit (OpCodes.Call, m1);
			il.Emit (OpCodes.Ldloc, loc);
			il.Emit (OpCodes.Call, m2);
			il.Emit (OpCodes.Ldstr, "1");
			il.Emit (OpCodes.Ret);

			var baked = tb.CreateType ();

			var x = Activator.CreateInstance (baked);
			var m = baked.GetMethod ("F");

			var s = m.Invoke (x, null);

			Assert.AreEqual ("1", s);

		}
	}
}