1 //
2 // DerivedType.cs - NUnit Test Cases for types derived from TypeBuilder
3 //
4 // Rodrigo Kumpera <rkumpera@novell.com>
5 //
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
7 //
8 
9 using System;
10 using System.Collections;
11 using System.Threading;
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.IO;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Runtime.InteropServices;
18 using NUnit.Framework;
19 using System.Runtime.CompilerServices;
20 
21 using System.Collections.Generic;
22 
23 namespace MonoTests.System.Reflection.Emit
24 {
25 	[TestFixture]
26 	public class PointerTypeTest
27 	{
28 		AssemblyBuilder assembly;
29 		ModuleBuilder module;
30 		int typeCount;
31 		static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
32 
MakeName()33 		string MakeName ()
34 		{
35 			return "internal__type"+ typeCount++;
36 		}
37 
38 		[SetUp]
SetUp()39 		protected void SetUp ()
40 		{
41 			AssemblyName assemblyName = new AssemblyName ();
42 			assemblyName.Name = ASSEMBLY_NAME;
43 
44 			assembly =
45 				Thread.GetDomain ().DefineDynamicAssembly (
46 					assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
47 
48 			module = assembly.DefineDynamicModule ("module1");
49 			typeCount = 0;
50 		}
51 
52 		[Test]
PropertiesValue()53 		public void PropertiesValue ()
54 		{
55 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
56 			Type ptr = tb.MakePointerType ();
57 
58 			Assert.AreEqual (assembly, ptr.Assembly, "#1");
59 			Assert.AreEqual ("ns.type*, " + assembly.FullName, ptr.AssemblyQualifiedName, "#2");
60 			Assert.AreEqual ("ns.type*", ptr.FullName, "#4");
61 			Assert.AreEqual (module, ptr.Module, "#5");
62 			Assert.AreEqual ("ns", ptr.Namespace, "#6");
63 			Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#7");
64 			Assert.AreEqual ("type*", ptr.Name, "#8");
65 
66 			try {
67 				object x = ptr.GUID;
68 				Assert.Fail ("#9");
69 			} catch (NotSupportedException) {}
70 
71 			try {
72 				object x = ptr.TypeHandle;
73 				Assert.Fail ("#10");
74 			} catch (NotSupportedException) {}
75 		}
76 
77 		[Test]
Methods()78 		public void Methods ()
79 		{
80 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
81 			Type ptr = tb.MakePointerType ();
82 
83 			try {
84 				ptr.GetInterface ("foo", true);
85 				Assert.Fail ("#1");
86 			} catch (NotSupportedException) {
87 
88 			}
89 
90 			try {
91 				ptr.GetInterfaces ();
92 				Assert.Fail ("#2");
93 			} catch (NotSupportedException) {
94 
95 			}
96 
97 			Assert.AreEqual (tb, ptr.GetElementType ());
98 
99 			try {
100 				ptr.GetEvent ("foo", BindingFlags.Public);
101 				Assert.Fail ("#4");
102 			} catch (NotSupportedException) {
103 
104 			}
105 
106 			try {
107 				ptr.GetEvents (BindingFlags.Public);
108 				Assert.Fail ("#5");
109 			} catch (NotSupportedException) {
110 
111 			}
112 
113 			try {
114 				ptr.GetField ("foo", BindingFlags.Public);
115 				Assert.Fail ("#6");
116 			} catch (NotSupportedException) {
117 
118 			}
119 
120 			try {
121 				ptr.GetFields (BindingFlags.Public);
122 				Assert.Fail ("#7");
123 			} catch (NotSupportedException) {
124 
125 			}
126 
127 			try {
128 				ptr.GetMembers (BindingFlags.Public);
129 				Assert.Fail ("#8");
130 			} catch (NotSupportedException) {
131 
132 			}
133 
134 			try {
135 				ptr.GetMethod ("Sort");
136 				Assert.Fail ("#9");
137 			} catch (NotSupportedException) {
138 
139 			}
140 
141 			try {
142 				ptr.GetMethods (BindingFlags.Public);
143 				Assert.Fail ("#9");
144 			} catch (NotSupportedException) {
145 
146 			}
147 
148 			try {
149 				ptr.GetNestedType ("bla", BindingFlags.Public);
150 				Assert.Fail ("#10");
151 			} catch (NotSupportedException) {
152 
153 			}
154 
155 			try {
156 				ptr.GetNestedTypes (BindingFlags.Public);
157 				Assert.Fail ("#11");
158 			} catch (NotSupportedException) {
159 
160 			}
161 
162 			try {
163 				ptr.GetProperties (BindingFlags.Public);
164 				Assert.Fail ("#12");
165 			} catch (NotSupportedException) {
166 
167 			}
168 
169 			try {
170 				ptr.GetProperty ("Length");
171 				Assert.Fail ("#13");
172 			} catch (NotSupportedException) {
173 
174 			}
175 
176 			try {
177 				ptr.GetConstructor (new Type[] { typeof (int) });
178 				Assert.Fail ("#14");
179 			} catch (NotSupportedException) {
180 
181 			}
182 
183 			TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
184 			Assert.AreEqual (attr, ptr.Attributes, "#15");
185 
186 			Assert.IsTrue (ptr.HasElementType, "#16");
187 			Assert.IsFalse (ptr.IsArray, "#17");
188 			Assert.IsFalse (ptr.IsByRef, "#18");
189 			Assert.IsFalse (ptr.IsCOMObject, "#19");
190 			Assert.IsTrue (ptr.IsPointer, "#20");
191 			Assert.IsFalse (ptr.IsPrimitive, "#21");
192 
193 			try {
194 				ptr.GetConstructors (BindingFlags.Public);
195 				Assert.Fail ("#22");
196 			} catch (NotSupportedException) {
197 
198 			}
199 
200 			try {
201 				ptr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
202 				Assert.Fail ("#23");
203 			} catch (NotSupportedException) {
204 
205 			}
206 
207 			try {
208 				ptr.GetArrayRank ();
209 				Assert.Fail ("#23");
210 			} catch (NotSupportedException) {
211 
212 			}
213 		}
214 
215 		[Test]
GenericTypeMembers()216 		public void GenericTypeMembers ()
217 		{
218 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
219 			Type arr = tb.MakeArrayType ();
220 
221 			try {
222 				arr.GetGenericArguments ();
223 				Assert.Fail ("#1");
224 			} catch (NotSupportedException) {}
225 
226 			try {
227 				arr.GetGenericParameterConstraints ();
228 				Assert.Fail ("#2");
229 			} catch (InvalidOperationException) {}
230 
231 			try {
232 				arr.GetGenericTypeDefinition ();
233 				Assert.Fail ("#3");
234 			} catch (NotSupportedException) {}
235 
236 			Assert.IsFalse (arr.ContainsGenericParameters, "#4");
237 			try {
238 				var x = arr.GenericParameterAttributes;
239 				Assert.Fail ("#5");
240 			} catch (NotSupportedException) {}
241 
242 			try {
243 				var x = arr.GenericParameterPosition;
244 				Assert.Fail ("#6");
245 			} catch (InvalidOperationException) {}
246 
247 			Assert.IsFalse (arr.ContainsGenericParameters, "#7");
248 
249 			Assert.IsFalse (arr.IsGenericParameter, "#8");
250 			Assert.IsFalse (arr.IsGenericType, "#9");
251 			Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
252 		}
253 
254 		[Test]
AttributeValues()255 		public void AttributeValues ()
256 		{
257 				TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
258 				Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
259 
260 				tb = module.DefineType (MakeName (), TypeAttributes.Public);
261 				Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
262 
263 				tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
264 				Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
265 
266 				tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
267 				Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
268 		}
269 
270 		[Test]
AsParamType()271 		public void AsParamType ()
272 		{
273 
274 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
275 			Type ptr = tb.MakePointerType ();
276 
277 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { ptr });
278 			ILGenerator ilgen = mb.GetILGenerator ();
279 			ilgen.Emit (OpCodes.Ret);
280 
281 			Type res = tb.CreateType ();
282 
283 			object o = Activator.CreateInstance (res);
284 			//FIXME this crashes the runtime
285 			//res.GetMethod ("Test").Invoke (o, new object[1] { null });
286 		}
287 
288 		[Test]
AsLocalVariable()289 		public void AsLocalVariable ()
290 		{
291 
292 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
293 			Type ptr = tb.MakePointerType ();
294 
295 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
296 			ILGenerator ilgen = mb.GetILGenerator ();
297 			ilgen.DeclareLocal (ptr);
298 			ilgen.Emit (OpCodes.Ret);
299 
300 			Type res = tb.CreateType ();
301 
302 			object o = Activator.CreateInstance (res);
303 			res.GetMethod ("Test").Invoke (o, null);
304 		}
305 
306 		[Test]
TestEquals()307 		public void TestEquals ()
308 		{
309 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
310 			Type ptr = tb.MakePointerType ();
311 			Type ptr2 = tb.MakePointerType ();
312 			Assert.IsTrue (ptr.Equals (ptr), "#2");
313 		}
314 
315 		[Test]
316 		[Category ("NotWorking")] //two stage type creation makes this fail
TestEquals2()317 		public void TestEquals2 ()
318 		{
319 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
320 			Type ptr = tb.MakePointerType ();
321 			Type ptr2 = tb.MakePointerType ();
322 			Assert.IsFalse (ptr.Equals (ptr2), "#1");
323 		}
324 
325 		[Test]
IsSubclassOf()326 		public void IsSubclassOf ()
327 		{
328 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
329 			Type ptr = tb.MakePointerType ();
330 			Assert.IsFalse (ptr.IsSubclassOf (tb), "#1");
331 			Assert.IsFalse (ptr.IsSubclassOf (typeof (object[])), "#2");
332 		}
333 
334 		[Test]
IsAssignableFrom()335 		public void IsAssignableFrom ()
336 		{
337 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
338 			Type ptr = tb.MakePointerType ();
339 			Assert.IsFalse (ptr.IsAssignableFrom (tb), "#1");
340 			Assert.IsFalse (ptr.IsAssignableFrom (typeof (object[])), "#2");
341 		}
342 
343 		[Test]
344 		[Category ("NotWorking")] //two stage type creation makes this fail
IsAssignableFrom2()345 		public void IsAssignableFrom2 ()
346 		{
347 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
348 			Type ptr = tb.MakePointerType ();
349 			Assert.IsFalse (typeof (object[]).IsAssignableFrom (ptr), "#1");
350 			Assert.IsFalse (typeof (object).IsAssignableFrom (ptr), "#2");
351 		}
352 
353 
354 		[Test]
GetInterfaceMap()355 		public void GetInterfaceMap ()
356 		{
357 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
358 			Type ptr = tb.MakePointerType ();
359 			try {
360 				ptr.GetInterfaceMap (typeof (IEnumerable));
361 				Assert.Fail ("#1");
362 			} catch (NotSupportedException) {
363 
364 			}
365 		}
366 
367 		[Test]
IsInstanceOfType()368 		public void IsInstanceOfType ()
369 		{
370 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
371 			Type ptr = tb.MakePointerType ();
372 			Assert.IsFalse (ptr.IsInstanceOfType (tb), "#1");
373 			Assert.IsFalse (ptr.IsInstanceOfType (null), "#2");
374 			Assert.IsFalse (ptr.IsInstanceOfType (new object [1]), "#3");
375 
376 			Type t = tb.CreateType ();
377 			object obj = Activator.CreateInstance (t);
378 			Assert.IsFalse (ptr.IsInstanceOfType (obj), "#4");
379 		}
380 
381 		[Test]
IsGenericTypeDefinition()382 		public void IsGenericTypeDefinition ()
383 		{
384 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
385 			Type ptr = tb.MakePointerType ();
386 			Assert.IsFalse (ptr.IsGenericTypeDefinition, "#1");
387 		}
388 
389 		[Test]
IsGenericType()390 		public void IsGenericType ()
391 		{
392 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
393 			Type ptr = tb.MakePointerType ();
394 			Assert.IsFalse (ptr.IsGenericType, "#1");
395 		}
396 
397 		[Test]
MakeGenericType()398 		public void MakeGenericType ()
399 		{
400 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
401 			Type ptr = tb.MakePointerType ();
402 			try {
403 				ptr.MakeGenericType (new Type[] { typeof (string) });
404 				Assert.Fail ("#1");
405 			} catch (NotSupportedException) {}
406 		}
407 
408 		[Test]
GenericParameterPosition()409 		public void GenericParameterPosition ()
410 		{
411 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
412 			Type ptr = tb.MakePointerType ();
413 			try {
414 				int pos = ptr.GenericParameterPosition;
415 				Assert.Fail ("#1");
416 			} catch (InvalidOperationException) {}
417 		}
418 
419 		[Test]
GenericParameterAttributes()420 		public void GenericParameterAttributes ()
421 		{
422 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
423 			Type ptr = tb.MakePointerType ();
424 			try {
425 				object attr = ptr.GenericParameterAttributes;
426 				Assert.Fail ("#1");
427 			} catch (NotSupportedException) {}
428 		}
429 
430 		[Test]
GetGenericParameterConstraints()431 		public void GetGenericParameterConstraints ()
432 		{
433 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
434 			Type ptr = tb.MakePointerType ();
435 			try {
436 				ptr.GetGenericParameterConstraints ();
437 				Assert.Fail ("#1");
438 			} catch (InvalidOperationException) {}
439 		}
440 
441 		[Test]
MakeArrayType()442 		public void MakeArrayType ()
443 		{
444 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
445 			Type ptr = tb.MakePointerType ();
446 			Type res = ptr.MakeArrayType ();
447 			Assert.IsNotNull (res, "#1");
448 			Assert.IsTrue (res.IsArray, "#2");
449 
450 			res = ptr.MakeArrayType (2);
451 			Assert.IsNotNull (res, "#3");
452 			Assert.IsTrue (res.IsArray, "#4");
453 		}
454 
455 		[Test]
MakeByRefType()456 		public void MakeByRefType ()
457 		{
458 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
459 			Type ptr = tb.MakePointerType ();
460 			Type res = ptr.MakeByRefType ();
461 
462 			Assert.IsNotNull (res, "#1");
463 			Assert.IsTrue (res.IsByRef, "#2");
464 		}
465 
466 		[Test]
MakePointerType()467 		public void MakePointerType ()
468 		{
469 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
470 			Type ptr = tb.MakePointerType ();
471 			Type res = ptr.MakePointerType ();
472 
473 			Assert.IsNotNull (res, "#1");
474 			Assert.IsTrue (res.IsPointer, "#2");
475 		}
476 
477 		[Test]
StructLayoutAttribute()478 		public void StructLayoutAttribute ()
479 		{
480 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
481 			Type ptr = tb.MakePointerType ();
482 			try {
483 				object x = ptr.StructLayoutAttribute;
484 				Assert.Fail ("#1");
485 			} catch (NotSupportedException) {}
486 		}
487 
488 		[Test]
ByRefOfGenericTypeParameter()489 		public void ByRefOfGenericTypeParameter ()
490 		{
491 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
492 			var gparam = tb.DefineGenericParameters ("F")[0];
493 			Type ptr = gparam.MakePointerType ();
494 
495 			try {
496 				ptr.GetGenericArguments ();
497 				Assert.Fail ("#1");
498 			} catch (NotSupportedException) {}
499 
500 			try {
501 				ptr.GetGenericParameterConstraints ();
502 				Assert.Fail ("#2");
503 			} catch (InvalidOperationException) {}
504 
505 			try {
506 				ptr.GetGenericTypeDefinition ();
507 				Assert.Fail ("#3");
508 			} catch (NotSupportedException) {}
509 
510 			Assert.IsTrue (ptr.ContainsGenericParameters, "#4");
511 			try {
512 				var x = ptr.GenericParameterAttributes;
513 				Assert.Fail ("#5");
514 			} catch (NotSupportedException) {}
515 
516 			try {
517 				var x = ptr.GenericParameterPosition;
518 				Assert.Fail ("#6");
519 			} catch (InvalidOperationException) {}
520 
521 
522 			Assert.IsFalse (ptr.IsGenericParameter, "#8");
523 			Assert.IsFalse (ptr.IsGenericType, "#9");
524 			Assert.IsFalse (ptr.IsGenericTypeDefinition, "#10");
525 
526 			Assert.AreEqual (TypeAttributes.Public, ptr.Attributes, "#11");
527 
528 			Assert.IsTrue (ptr.HasElementType, "#12");
529 			Assert.IsTrue (ptr.IsPointer, "#13");
530 
531 			Assert.AreEqual (assembly, ptr.Assembly, "#14");
532 			Assert.AreEqual (null, ptr.AssemblyQualifiedName, "#15");
533 			//XXX LAMEIMPL this passes on MS even thou it's pretty much very wrong.
534 			Assert.AreEqual (typeof (Array), ptr.BaseType, "#16");
535 			Assert.AreEqual (null, ptr.FullName, "#17");
536 			Assert.AreEqual (module, ptr.Module, "#18");
537 			Assert.AreEqual (null, ptr.Namespace, "#19");
538 			Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#20");
539 			Assert.AreEqual ("F*", ptr.Name, "#21");
540 
541 			Assert.AreEqual (gparam, ptr.GetElementType (), "#22");
542 		}
543 	}
544 
545 
546 	[TestFixture]
547 	public class ByrefTypeTest
548 	{
549 		AssemblyBuilder assembly;
550 		ModuleBuilder module;
551 		int typeCount;
552 		static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
553 
MakeName()554 		string MakeName ()
555 		{
556 			return "internal__type"+ typeCount++;
557 		}
558 
559 		[SetUp]
SetUp()560 		protected void SetUp ()
561 		{
562 			SetUp (AssemblyBuilderAccess.RunAndSave);
563 		}
564 
SetUp(AssemblyBuilderAccess mode)565 		protected void SetUp (AssemblyBuilderAccess mode)
566 		{
567 			AssemblyName assemblyName = new AssemblyName ();
568 			assemblyName.Name = ASSEMBLY_NAME;
569 
570 			assembly =
571 				Thread.GetDomain ().DefineDynamicAssembly (
572 					assemblyName, mode, Path.GetTempPath ());
573 
574 			module = assembly.DefineDynamicModule ("module1");
575 			typeCount = 0;
576 		}
577 
578 		[Test]
PropertiesValue()579 		public void PropertiesValue ()
580 		{
581 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
582 			Type byref = tb.MakeByRefType ();
583 
584 			Assert.AreEqual (assembly, byref.Assembly, "#1");
585 			Assert.AreEqual ("ns.type&, " + assembly.FullName, byref.AssemblyQualifiedName, "#2");
586 			Assert.AreEqual ("ns.type&", byref.FullName, "#4");
587 			Assert.AreEqual (module, byref.Module, "#5");
588 			Assert.AreEqual ("ns", byref.Namespace, "#6");
589 			Assert.AreEqual (byref, byref.UnderlyingSystemType, "#7");
590 			Assert.AreEqual ("type&", byref.Name, "#8");
591 
592 			try {
593 				object x = byref.GUID;
594 				Assert.Fail ("#9");
595 			} catch (NotSupportedException) {}
596 
597 			try {
598 				object x = byref.TypeHandle;
599 				Assert.Fail ("#10");
600 			} catch (NotSupportedException) {}
601 		}
602 
603 		[Test]
Methods()604 		public void Methods ()
605 		{
606 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
607 			Type byref = tb.MakeByRefType ();
608 
609 			try {
610 				byref.GetInterface ("foo", true);
611 				Assert.Fail ("#1");
612 			} catch (NotSupportedException) {
613 
614 			}
615 
616 			try {
617 				byref.GetInterfaces ();
618 				Assert.Fail ("#2");
619 			} catch (NotSupportedException) {
620 
621 			}
622 
623 			Assert.AreEqual (tb, byref.GetElementType ());
624 
625 			try {
626 				byref.GetEvent ("foo", BindingFlags.Public);
627 				Assert.Fail ("#4");
628 			} catch (NotSupportedException) {
629 
630 			}
631 
632 			try {
633 				byref.GetEvents (BindingFlags.Public);
634 				Assert.Fail ("#5");
635 			} catch (NotSupportedException) {
636 
637 			}
638 
639 			try {
640 				byref.GetField ("foo", BindingFlags.Public);
641 				Assert.Fail ("#6");
642 			} catch (NotSupportedException) {
643 
644 			}
645 
646 			try {
647 				byref.GetFields (BindingFlags.Public);
648 				Assert.Fail ("#7");
649 			} catch (NotSupportedException) {
650 
651 			}
652 
653 			try {
654 				byref.GetMembers (BindingFlags.Public);
655 				Assert.Fail ("#8");
656 			} catch (NotSupportedException) {
657 
658 			}
659 
660 			try {
661 				byref.GetMethod ("Sort");
662 				Assert.Fail ("#9");
663 			} catch (NotSupportedException) {
664 
665 			}
666 
667 			try {
668 				byref.GetMethods (BindingFlags.Public);
669 				Assert.Fail ("#9");
670 			} catch (NotSupportedException) {
671 
672 			}
673 
674 			try {
675 				byref.GetNestedType ("bla", BindingFlags.Public);
676 				Assert.Fail ("#10");
677 			} catch (NotSupportedException) {
678 
679 			}
680 
681 			try {
682 				byref.GetNestedTypes (BindingFlags.Public);
683 				Assert.Fail ("#11");
684 			} catch (NotSupportedException) {
685 
686 			}
687 
688 			try {
689 				byref.GetProperties (BindingFlags.Public);
690 				Assert.Fail ("#12");
691 			} catch (NotSupportedException) {
692 
693 			}
694 
695 			try {
696 				byref.GetProperty ("Length");
697 				Assert.Fail ("#13");
698 			} catch (NotSupportedException) {
699 
700 			}
701 
702 			try {
703 				byref.GetConstructor (new Type[] { typeof (int) });
704 				Assert.Fail ("#14");
705 			} catch (NotSupportedException) {
706 
707 			}
708 
709 			TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
710 			Assert.AreEqual (attr, byref.Attributes, "#15");
711 
712 			Assert.IsTrue (byref.HasElementType, "#16");
713 			Assert.IsFalse (byref.IsArray, "#17");
714 			Assert.IsTrue (byref.IsByRef, "#18");
715 			Assert.IsFalse (byref.IsCOMObject, "#19");
716 			Assert.IsFalse (byref.IsPointer, "#20");
717 			Assert.IsFalse (byref.IsPrimitive, "#21");
718 
719 			try {
720 				byref.GetConstructors (BindingFlags.Public);
721 				Assert.Fail ("#22");
722 			} catch (NotSupportedException) {
723 
724 			}
725 
726 			try {
727 				byref.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
728 				Assert.Fail ("#23");
729 			} catch (NotSupportedException) {
730 
731 			}
732 
733 			try {
734 				byref.GetArrayRank ();
735 				Assert.Fail ("#23");
736 			} catch (NotSupportedException) {
737 
738 			}
739 		}
740 
741 		[Test]
AttributeValues()742 		public void AttributeValues ()
743 		{
744 				TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
745 				Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
746 
747 				tb = module.DefineType (MakeName (), TypeAttributes.Public);
748 				Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
749 
750 				tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
751 				Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
752 
753 				tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
754 				Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
755 		}
756 
757 		[Test]
AsParamType()758 		public void AsParamType ()
759 		{
760 
761 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
762 			Type byref = tb.MakeByRefType ();
763 
764 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { byref });
765 			ILGenerator ilgen = mb.GetILGenerator ();
766 			ilgen.Emit (OpCodes.Ret);
767 
768 			Type res = tb.CreateType ();
769 
770 			object o = Activator.CreateInstance (res);
771 			res.GetMethod ("Test").Invoke (o, new object[1] { null });
772 		}
773 
774 		[Test]
AsLocalVariable()775 		public void AsLocalVariable ()
776 		{
777 
778 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
779 			Type byref = tb.MakeByRefType ();
780 
781 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
782 			ILGenerator ilgen = mb.GetILGenerator ();
783 			ilgen.DeclareLocal (byref);
784 			ilgen.Emit (OpCodes.Ret);
785 
786 			Type res = tb.CreateType ();
787 
788 			object o = Activator.CreateInstance (res);
789 			res.GetMethod ("Test").Invoke (o, null);
790 		}
791 
792 		[Test]
TestEquals()793 		public void TestEquals ()
794 		{
795 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
796 			Type byref = tb.MakeByRefType ();
797 			Type byref2 = tb.MakeByRefType ();
798 			Assert.IsTrue (byref.Equals (byref), "#1");
799 		}
800 
801 		[Test]
802 		[Category ("NotWorking")] //two stage type creation makes this fail
TestEquals2()803 		public void TestEquals2 ()
804 		{
805 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
806 			Type byref = tb.MakeByRefType ();
807 			Type byref2 = tb.MakeByRefType ();
808 			Assert.IsFalse (byref.Equals (byref2), "#1");
809 		}
810 
811 		[Test]
IsSubclassOf()812 		public void IsSubclassOf ()
813 		{
814 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
815 			Type byref = tb.MakeByRefType ();
816 			Assert.IsFalse (byref.IsSubclassOf (tb), "#1");
817 			Assert.IsFalse (byref.IsSubclassOf (typeof (object[])), "#2");
818 		}
819 
820 		[Test]
IsAssignableFrom()821 		public void IsAssignableFrom ()
822 		{
823 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
824 			Type byref = tb.MakeByRefType ();
825 			Assert.IsFalse (byref.IsAssignableFrom (tb), "#1");
826 			Assert.IsFalse (byref.IsAssignableFrom (typeof (object[])), "#2");
827 		}
828 
829 		[Test]
830 		[Category ("NotWorking")] //two stage type creation makes this fail
IsAssignableFrom2()831 		public void IsAssignableFrom2 ()
832 		{
833 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
834 			Type byref = tb.MakeByRefType ();
835 			Assert.IsFalse (typeof (object[]).IsAssignableFrom (byref), "#1");
836 			Assert.IsFalse (typeof (object).IsAssignableFrom (byref), "#2");
837 		}
838 
839 		[Test]
GetInterfaceMap()840 		public void GetInterfaceMap ()
841 		{
842 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
843 			Type byref = tb.MakeByRefType ();
844 			try {
845 				byref.GetInterfaceMap (typeof (IEnumerable));
846 				Assert.Fail ("#1");
847 			} catch (NotSupportedException) {
848 
849 			}
850 		}
851 
852 		[Test]
IsInstanceOfType()853 		public void IsInstanceOfType ()
854 		{
855 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
856 			Type byref = tb.MakeByRefType ();
857 			Assert.IsFalse (byref.IsInstanceOfType (tb), "#1");
858 			Assert.IsFalse (byref.IsInstanceOfType (null), "#2");
859 			Assert.IsFalse (byref.IsInstanceOfType (new object [1]), "#3");
860 
861 			Type t = tb.CreateType ();
862 			object obj = Activator.CreateInstance (t);
863 			Assert.IsFalse (byref.IsInstanceOfType (obj), "#4");
864 		}
865 
866 		[Test]
IsGenericTypeDefinition()867 		public void IsGenericTypeDefinition ()
868 		{
869 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
870 			Type byref = tb.MakeByRefType ();
871 			Assert.IsFalse (byref.IsGenericTypeDefinition, "#1");
872 		}
873 
874 		[Test]
IsGenericType()875 		public void IsGenericType ()
876 		{
877 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
878 			Type byref = tb.MakeByRefType ();
879 			Assert.IsFalse (byref.IsGenericType, "#1");
880 		}
881 
882 		[Test]
MakeGenericType()883 		public void MakeGenericType ()
884 		{
885 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
886 			Type byref = tb.MakeByRefType ();
887 			try {
888 				byref.MakeGenericType (new Type[] { typeof (string) });
889 				Assert.Fail ("#1");
890 			} catch (NotSupportedException) {}
891 		}
892 
893 		[Test]
GenericParameterPosition()894 		public void GenericParameterPosition ()
895 		{
896 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
897 			Type byref = tb.MakeByRefType ();
898 			try {
899 				int pos = byref.GenericParameterPosition;
900 				Assert.Fail ("#1");
901 			} catch (InvalidOperationException) {}
902 		}
903 
904 		[Test]
GenericParameterAttributes()905 		public void GenericParameterAttributes ()
906 		{
907 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
908 			Type byref = tb.MakeByRefType ();
909 			try {
910 				object attr = byref.GenericParameterAttributes;
911 				Assert.Fail ("#1");
912 			} catch (NotSupportedException) {}
913 		}
914 
915 		[Test]
GetGenericParameterConstraints()916 		public void GetGenericParameterConstraints ()
917 		{
918 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
919 			Type byref = tb.MakeByRefType ();
920 			try {
921 				byref.GetGenericParameterConstraints ();
922 				Assert.Fail ("#1");
923 			} catch (InvalidOperationException) {}
924 		}
925 
926 		[Test]
MakeArrayType()927 		public void MakeArrayType ()
928 		{
929 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
930 			Type byref = tb.MakeByRefType ();
931 			try {
932 				byref.MakeArrayType ();
933 				Assert.Fail ("#1");
934 			} catch (ArgumentException) {};
935 			try {
936 				byref.MakeArrayType (2);
937 				Assert.Fail ("#2");
938 			} catch (ArgumentException) {};
939 		}
940 
941 		[Test]
MakeByRefType()942 		public void MakeByRefType ()
943 		{
944 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
945 			Type byref = tb.MakeByRefType ();
946 			try {
947 				byref.MakeByRefType ();
948 				Assert.Fail ("#1");
949 			} catch (ArgumentException) {}
950 		}
951 
952 		[Test]
MakePointerType()953 		public void MakePointerType ()
954 		{
955 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
956 			Type byref = tb.MakeByRefType ();
957 			try {
958 				byref.MakePointerType ();
959 				Assert.Fail ("#1");
960 			} catch (ArgumentException) {}
961 		}
962 
963 		[Test]
StructLayoutAttribute()964 		public void StructLayoutAttribute ()
965 		{
966 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
967 			Type byref = tb.MakeByRefType ();
968 			try {
969 				object x = byref.StructLayoutAttribute;
970 				Assert.Fail ("#1");
971 			} catch (NotSupportedException) {}
972 		}
973 
974 		[Test]
975 		[Category ("NotDotNet")]
976 		// CompilerContext no longer supported
977 		[Category ("NotWorking")]
ByRefOfAttriburesUnderCompilerContext()978 		public void ByRefOfAttriburesUnderCompilerContext ()
979 		{
980 			SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
981 
982 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
983 			var gparam = tb.DefineGenericParameters ("F")[0];
984 			Type byref = gparam.MakeByRefType ();
985 
986 			tb = module.DefineType (MakeName (), TypeAttributes.Public);
987 			Assert.AreEqual (TypeAttributes.Public , byref.Attributes, "#1");
988 
989 		}
990 
991 		[Test]
ByRefOfGenericTypeParameter()992 		public void ByRefOfGenericTypeParameter ()
993 		{
994 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
995 			var gparam = tb.DefineGenericParameters ("F")[0];
996 			Type byref = gparam.MakeByRefType ();
997 
998 			try {
999 				byref.GetGenericArguments ();
1000 				Assert.Fail ("#1");
1001 			} catch (NotSupportedException) {}
1002 
1003 			try {
1004 				byref.GetGenericParameterConstraints ();
1005 				Assert.Fail ("#2");
1006 			} catch (InvalidOperationException) {}
1007 
1008 			try {
1009 				byref.GetGenericTypeDefinition ();
1010 				Assert.Fail ("#3");
1011 			} catch (NotSupportedException) {}
1012 
1013 			Assert.IsTrue (byref.ContainsGenericParameters, "#4");
1014 			try {
1015 				var x = byref.GenericParameterAttributes;
1016 				Assert.Fail ("#5");
1017 			} catch (NotSupportedException) {}
1018 
1019 			try {
1020 				var x = byref.GenericParameterPosition;
1021 				Assert.Fail ("#6");
1022 			} catch (InvalidOperationException) {}
1023 
1024 
1025 			Assert.IsFalse (byref.IsGenericParameter, "#8");
1026 			Assert.IsFalse (byref.IsGenericType, "#9");
1027 			Assert.IsFalse (byref.IsGenericTypeDefinition, "#10");
1028 
1029 
1030 			Assert.AreEqual (TypeAttributes.Public, byref.Attributes, "#11");
1031 
1032 			Assert.IsTrue (byref.HasElementType, "#12");
1033 			Assert.IsTrue (byref.IsByRef, "#13");
1034 
1035 			Assert.AreEqual (assembly, byref.Assembly, "#14");
1036 			Assert.AreEqual (null, byref.AssemblyQualifiedName, "#15");
1037 			//XXX LAMEIMPL this passes on MS even thou it's pretty much very wrong.
1038 			Assert.AreEqual (typeof (Array), byref.BaseType, "#16");
1039 			Assert.AreEqual (null, byref.FullName, "#17");
1040 			Assert.AreEqual (module, byref.Module, "#18");
1041 			Assert.AreEqual (null, byref.Namespace, "#19");
1042 			Assert.AreEqual (byref, byref.UnderlyingSystemType, "#20");
1043 			Assert.AreEqual ("F&", byref.Name, "#21");
1044 
1045 			Assert.AreEqual (gparam, byref.GetElementType (), "#22");
1046 		}
1047 	}
1048 
1049 	[TestFixture]
1050 	public class ArrayTypeTest
1051 	{
1052 		AssemblyBuilder assembly;
1053 		ModuleBuilder module;
1054 		int typeCount;
1055 		static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
1056 
MakeName()1057 		string MakeName ()
1058 		{
1059 			return "internal__type"+ typeCount++;
1060 		}
1061 
1062 
1063 		[SetUp]
SetUp()1064 		protected void SetUp ()
1065 		{
1066 			SetUp (AssemblyBuilderAccess.RunAndSave);
1067 		}
1068 
SetUp(AssemblyBuilderAccess mode)1069 		protected void SetUp (AssemblyBuilderAccess mode)
1070 		{
1071 			AssemblyName assemblyName = new AssemblyName ();
1072 			assemblyName.Name = ASSEMBLY_NAME;
1073 
1074 			assembly =
1075 				Thread.GetDomain ().DefineDynamicAssembly (
1076 					assemblyName, mode, Path.GetTempPath ());
1077 
1078 			module = assembly.DefineDynamicModule ("module1");
1079 			typeCount = 0;
1080 		}
1081 
1082 		[Test]
OneDimMultiDimentionArray()1083 		public void OneDimMultiDimentionArray ()
1084 		{
1085 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1086 
1087 			Type arr1 = tb.MakeArrayType ();
1088 			Type arr2 = tb.MakeArrayType (1);
1089 			Type arr3 = arr1.MakeArrayType (1);
1090 
1091 			Assert.AreEqual ("type[]", arr1.Name, "#1");
1092 			Assert.AreEqual ("type[*]", arr2.Name, "#2");
1093 			Assert.AreEqual ("type[][*]", arr3.Name, "#3");
1094 
1095 			var gparam = tb.DefineGenericParameters ("F")[0];
1096 			Type arr4 = gparam.MakeArrayType ();
1097 			Type arr5 = gparam.MakeArrayType (1);
1098 
1099 			Assert.AreEqual ("F[]", arr4.Name, "#4");
1100 			Assert.AreEqual ("F[*]", arr5.Name, "#5");
1101 
1102 			var eb = module.DefineEnum ("enum", TypeAttributes.Public, tb);
1103 			Type arr6 = eb.MakeArrayType ();
1104 			Type arr7 = eb.MakeArrayType (1);
1105 
1106 			Assert.AreEqual ("enum[]", arr6.Name, "#6");
1107 			Assert.AreEqual ("enum[*]", arr7.Name, "#7");
1108 		}
1109 
1110 		[Test]
PropertiesValue()1111 		public void PropertiesValue ()
1112 		{
1113 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1114 			Type arr = tb.MakeArrayType ();
1115 
1116 			Assert.AreEqual (assembly, arr.Assembly, "#1");
1117 			Assert.AreEqual ("ns.type[], " + assembly.FullName, arr.AssemblyQualifiedName, "#2");
1118 			Assert.AreEqual (typeof (Array), arr.BaseType, "#3");
1119 			Assert.AreEqual ("ns.type[]", arr.FullName, "#4");
1120 			Assert.AreEqual (module, arr.Module, "#5");
1121 			Assert.AreEqual ("ns", arr.Namespace, "#6");
1122 			Assert.AreEqual (arr, arr.UnderlyingSystemType, "#7");
1123 			Assert.AreEqual ("type[]", arr.Name, "#8");
1124 
1125 			try {
1126 				object x = arr.GUID;
1127 				Assert.Fail ("#9");
1128 			} catch (NotSupportedException) {}
1129 
1130 			try {
1131 				object x = arr.TypeHandle;
1132 				Assert.Fail ("#10");
1133 			} catch (NotSupportedException) {}
1134 		}
1135 
1136 		[Test]
Methods()1137 		public void Methods ()
1138 		{
1139 			TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
1140 			Type arr = tb.MakeArrayType ();
1141 
1142 			try {
1143 				arr.GetInterface ("foo", true);
1144 				Assert.Fail ("#1");
1145 			} catch (NotSupportedException) {
1146 
1147 			}
1148 
1149 			try {
1150 				arr.GetInterfaces ();
1151 				Assert.Fail ("#2");
1152 			} catch (NotSupportedException) {
1153 
1154 			}
1155 
1156 			Assert.AreEqual (tb, arr.GetElementType ());
1157 
1158 			try {
1159 				arr.GetEvent ("foo", BindingFlags.Public);
1160 				Assert.Fail ("#4");
1161 			} catch (NotSupportedException) {
1162 
1163 			}
1164 
1165 			try {
1166 				arr.GetEvents (BindingFlags.Public);
1167 				Assert.Fail ("#5");
1168 			} catch (NotSupportedException) {
1169 
1170 			}
1171 
1172 			try {
1173 				arr.GetField ("foo", BindingFlags.Public);
1174 				Assert.Fail ("#6");
1175 			} catch (NotSupportedException) {
1176 
1177 			}
1178 
1179 			try {
1180 				arr.GetFields (BindingFlags.Public);
1181 				Assert.Fail ("#7");
1182 			} catch (NotSupportedException) {
1183 
1184 			}
1185 
1186 			try {
1187 				arr.GetMembers (BindingFlags.Public);
1188 				Assert.Fail ("#8");
1189 			} catch (NotSupportedException) {
1190 
1191 			}
1192 
1193 			try {
1194 				arr.GetMethod ("Sort");
1195 				Assert.Fail ("#9");
1196 			} catch (NotSupportedException) {
1197 
1198 			}
1199 
1200 			try {
1201 				arr.GetMethods (BindingFlags.Public);
1202 				Assert.Fail ("#9");
1203 			} catch (NotSupportedException) {
1204 
1205 			}
1206 
1207 			try {
1208 				arr.GetNestedType ("bla", BindingFlags.Public);
1209 				Assert.Fail ("#10");
1210 			} catch (NotSupportedException) {
1211 
1212 			}
1213 
1214 			try {
1215 				arr.GetNestedTypes (BindingFlags.Public);
1216 				Assert.Fail ("#11");
1217 			} catch (NotSupportedException) {
1218 
1219 			}
1220 
1221 			try {
1222 				arr.GetProperties (BindingFlags.Public);
1223 				Assert.Fail ("#12");
1224 			} catch (NotSupportedException) {
1225 
1226 			}
1227 
1228 			try {
1229 				arr.GetProperty ("Length");
1230 				Assert.Fail ("#13");
1231 			} catch (NotSupportedException) {
1232 
1233 			}
1234 
1235 			try {
1236 				arr.GetConstructor (new Type[] { typeof (int) });
1237 				Assert.Fail ("#14");
1238 			} catch (NotSupportedException) {
1239 
1240 			}
1241 
1242 			TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
1243 			Assert.AreEqual (attr, arr.Attributes, "#15");
1244 
1245 			Assert.IsTrue (arr.HasElementType, "#16");
1246 			Assert.IsTrue (arr.IsArray, "#17");
1247 			Assert.IsFalse (arr.IsByRef, "#18");
1248 			Assert.IsFalse (arr.IsCOMObject, "#19");
1249 			Assert.IsFalse (arr.IsPointer, "#20");
1250 			Assert.IsFalse (arr.IsPrimitive, "#21");
1251 
1252 			try {
1253 				arr.GetConstructors (BindingFlags.Public);
1254 				Assert.Fail ("#22");
1255 			} catch (NotSupportedException) {
1256 
1257 			}
1258 
1259 			try {
1260 				arr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
1261 				Assert.Fail ("#23");
1262 			} catch (NotSupportedException) {
1263 
1264 			}
1265 		}
1266 
1267 		[Test]
AttributeValues()1268 		public void AttributeValues ()
1269 		{
1270 			TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1271 			Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
1272 
1273 			tb = module.DefineType (MakeName (), TypeAttributes.Public);
1274 			Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
1275 
1276 			tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1277 			Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
1278 
1279 			tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1280 			Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
1281 		}
1282 
1283 		[Test]
1284 		[Category ("NotDotNet")]
1285 		// CompilerContext no longer supported
1286 		[Category ("NotWorking")]
AttributeValuesUnderCompilerContext()1287 		public void AttributeValuesUnderCompilerContext ()
1288 		{
1289 			SetUp (AssemblyBuilderAccess.RunAndSave | (AssemblyBuilderAccess)0x800);
1290 
1291 			TypeAttributes arrayAttr = TypeAttributes.Sealed | TypeAttributes.Serializable;
1292 
1293 			TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1294 			Assert.AreEqual (TypeAttributes.NotPublic | arrayAttr, tb.MakeArrayType ().Attributes, "#1");
1295 
1296 			tb = module.DefineType (MakeName (), TypeAttributes.Public);
1297 			Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "#2");
1298 
1299 			tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1300 			Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "#3");
1301 
1302 			tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1303 			Assert.AreEqual (TypeAttributes.Public | arrayAttr, tb.MakeArrayType ().Attributes, "$4");
1304 		}
1305 
1306 		[Test]
AsReturnType()1307 		public void AsReturnType ()
1308 		{
1309 
1310 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1311 			Type arr = tb.MakeArrayType ();
1312 
1313 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1314 			ILGenerator ilgen = mb.GetILGenerator ();
1315 			ilgen.Emit (OpCodes.Ldnull);
1316 			ilgen.Emit (OpCodes.Ret);
1317 
1318 			Type res = tb.CreateType ();
1319 
1320 			object o = Activator.CreateInstance (res);
1321 			res.GetMethod ("Test").Invoke (o, null);
1322 		}
1323 
1324 		[Test]
AsParamType()1325 		public void AsParamType ()
1326 		{
1327 
1328 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1329 			Type arr = tb.MakeArrayType ();
1330 
1331 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { arr });
1332 			ILGenerator ilgen = mb.GetILGenerator ();
1333 			ilgen.Emit (OpCodes.Ret);
1334 
1335 			Type res = tb.CreateType ();
1336 
1337 			object o = Activator.CreateInstance (res);
1338 			res.GetMethod ("Test").Invoke (o, new object[1] { null });
1339 		}
1340 
1341 		[Test]
AsLocalVariable()1342 		public void AsLocalVariable ()
1343 		{
1344 
1345 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1346 			Type arr = tb.MakeArrayType ();
1347 
1348 			MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1349 			ILGenerator ilgen = mb.GetILGenerator ();
1350 			ilgen.DeclareLocal (arr);
1351 			ilgen.Emit (OpCodes.Ldnull);
1352 			ilgen.Emit (OpCodes.Stloc_0);
1353 			ilgen.Emit (OpCodes.Ldloc_0);
1354 			ilgen.Emit (OpCodes.Ret);
1355 
1356 			Type res = tb.CreateType ();
1357 
1358 			object o = Activator.CreateInstance (res);
1359 			res.GetMethod ("Test").Invoke (o, null);
1360 		}
1361 
1362 		[Test]
TestEquals()1363 		public void TestEquals ()
1364 		{
1365 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1366 			Type arr = tb.MakeArrayType ();
1367 			Type arr2 = tb.MakeArrayType ();
1368 			Assert.IsTrue (arr.Equals (arr), "#1");
1369 		}
1370 
1371 		[Test]
1372 		[Category ("NotWorking")] //two stage type creation makes this fail
TestEquals2()1373 		public void TestEquals2 ()
1374 		{
1375 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1376 			Type arr = tb.MakeArrayType ();
1377 			Type arr2 = tb.MakeArrayType ();
1378 			Assert.IsFalse (arr.Equals (arr2), "#1");
1379 		}
1380 
1381 		[Test]
IsSubclassOf()1382 		public void IsSubclassOf ()
1383 		{
1384 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1385 			Type arr = tb.MakeArrayType ();
1386 			Assert.IsFalse (arr.IsSubclassOf (tb), "#1");
1387 			Assert.IsFalse (arr.IsSubclassOf (typeof (object[])), "#2");
1388 		}
1389 
1390 		[Test]
IsAssignableFrom()1391 		public void IsAssignableFrom ()
1392 		{
1393 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1394 			Type arr = tb.MakeArrayType ();
1395 			Assert.IsFalse (arr.IsAssignableFrom (tb), "#1");
1396 			Assert.IsFalse (arr.IsAssignableFrom (typeof (object[])), "#2");
1397 		}
1398 
1399 		[Test]
1400 		[Category ("NotWorking")] //two stage type creation makes this fail
IsAssignableFrom2()1401 		public void IsAssignableFrom2 ()
1402 		{
1403 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1404 			Type arr = tb.MakeArrayType ();
1405 			Assert.IsFalse (typeof (object[]).IsAssignableFrom (arr), "#1");
1406 			Assert.IsFalse (typeof (object).IsAssignableFrom (arr), "#2");
1407 		}
1408 
1409 		[Test]
GetInterfaceMap()1410 		public void GetInterfaceMap ()
1411 		{
1412 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1413 			Type arr = tb.MakeArrayType ();
1414 			try {
1415 				arr.GetInterfaceMap (typeof (IEnumerable));
1416 				Assert.Fail ("#1");
1417 			} catch (NotSupportedException) {
1418 
1419 			}
1420 		}
1421 
1422 		[Test]
IsInstanceOfType()1423 		public void IsInstanceOfType ()
1424 		{
1425 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1426 			Type arr = tb.MakeArrayType ();
1427 			Assert.IsFalse (arr.IsInstanceOfType (tb), "#1");
1428 			Assert.IsFalse (arr.IsInstanceOfType (null), "#2");
1429 			Assert.IsFalse (arr.IsInstanceOfType (new object [1]), "#3");
1430 
1431 			Type t = tb.CreateType ();
1432 			object obj = Array.CreateInstance (t, 10);
1433 			Assert.IsFalse (arr.IsInstanceOfType (obj), "#4");
1434 		}
1435 
1436 		[Test]
IsGenericTypeDefinition()1437 		public void IsGenericTypeDefinition ()
1438 		{
1439 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1440 			Type arr = tb.MakeArrayType ();
1441 			Assert.IsFalse (arr.IsGenericTypeDefinition, "#1");
1442 		}
1443 
1444 		[Test]
IsGenericType()1445 		public void IsGenericType ()
1446 		{
1447 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1448 			Type arr = tb.MakeArrayType ();
1449 			Assert.IsFalse (arr.IsGenericType, "#1");
1450 		}
1451 
1452 		[Test]
MakeGenericType()1453 		public void MakeGenericType ()
1454 		{
1455 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1456 			Type arr = tb.MakeArrayType ();
1457 			try {
1458 				arr.MakeGenericType (new Type[] { typeof (string) });
1459 				Assert.Fail ("#1");
1460 			} catch (NotSupportedException) {}
1461 		}
1462 
1463 		[Test]
GenericParameterPosition()1464 		public void GenericParameterPosition ()
1465 		{
1466 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1467 			Type arr = tb.MakeArrayType ();
1468 			try {
1469 				int pos = arr.GenericParameterPosition;
1470 				Assert.Fail ("#1");
1471 			} catch (InvalidOperationException) {}
1472 		}
1473 
1474 		[Test]
GenericParameterAttributes()1475 		public void GenericParameterAttributes ()
1476 		{
1477 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1478 			Type arr = tb.MakeArrayType ();
1479 			try {
1480 				object attr = arr.GenericParameterAttributes;
1481 				Assert.Fail ("#1");
1482 			} catch (NotSupportedException) {}
1483 		}
1484 
1485 		[Test]
GetGenericParameterConstraints()1486 		public void GetGenericParameterConstraints ()
1487 		{
1488 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1489 			Type arr = tb.MakeArrayType ();
1490 			try {
1491 				arr.GetGenericParameterConstraints ();
1492 				Assert.Fail ("#1");
1493 			} catch (InvalidOperationException) {}
1494 		}
1495 
1496 		[Test]
MakeArrayType()1497 		public void MakeArrayType ()
1498 		{
1499 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1500 			Type arr = tb.MakeArrayType ();
1501 			Type res = arr.MakeArrayType ();
1502 			Assert.IsNotNull (res, "#1");
1503 			Assert.IsTrue (res.IsArray, "#2");
1504 
1505 			res = arr.MakeArrayType (2);
1506 			Assert.IsNotNull (res, "#3");
1507 			Assert.IsTrue (res.IsArray, "#4");
1508 		}
1509 
1510 		[Test]
MakeByRefType()1511 		public void MakeByRefType ()
1512 		{
1513 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1514 			Type arr = tb.MakeArrayType ();
1515 			Type res = arr.MakeByRefType ();
1516 
1517 			Assert.IsNotNull (res, "#1");
1518 			Assert.IsTrue (res.IsByRef, "#2");
1519 		}
1520 
1521 		[Test]
MakePointerType()1522 		public void MakePointerType ()
1523 		{
1524 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1525 			Type arr = tb.MakeArrayType ();
1526 			Type res = arr.MakePointerType ();
1527 
1528 			Assert.IsNotNull (res, "#1");
1529 			Assert.IsTrue (res.IsPointer, "#2");
1530 		}
1531 
1532 		[Test]
StructLayoutAttribute()1533 		public void StructLayoutAttribute ()
1534 		{
1535 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1536 			Type arr = tb.MakeArrayType ();
1537 			try {
1538 				object x = arr.StructLayoutAttribute;
1539 				Assert.Fail ("#1");
1540 			} catch (NotSupportedException) {}
1541 		}
1542 
1543 		[Test]
GetArrayRank()1544 		public void GetArrayRank ()
1545 		{
1546 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1547 
1548 			Assert.AreEqual (1, tb.MakeArrayType ().GetArrayRank (), "#1");
1549 			Assert.AreEqual (2, tb.MakeArrayType (2).GetArrayRank (), "#2");
1550 		}
1551 
1552 		[Test]
GenericTypeMembers()1553 		public void GenericTypeMembers ()
1554 		{
1555 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1556 			Type arr = tb.MakeArrayType ();
1557 
1558 			try {
1559 				arr.GetGenericArguments ();
1560 				Assert.Fail ("#1");
1561 			} catch (NotSupportedException) {}
1562 
1563 			try {
1564 				arr.GetGenericParameterConstraints ();
1565 				Assert.Fail ("#2");
1566 			} catch (InvalidOperationException) {}
1567 
1568 			try {
1569 				arr.GetGenericTypeDefinition ();
1570 				Assert.Fail ("#3");
1571 			} catch (NotSupportedException) {}
1572 
1573 			Assert.IsFalse (arr.ContainsGenericParameters, "#4");
1574 			try {
1575 				var x = arr.GenericParameterAttributes;
1576 				Assert.Fail ("#5");
1577 			} catch (NotSupportedException) {}
1578 
1579 			try {
1580 				var x = arr.GenericParameterPosition;
1581 				Assert.Fail ("#6");
1582 			} catch (InvalidOperationException) {}
1583 
1584 			Assert.IsFalse (arr.ContainsGenericParameters, "#7");
1585 
1586 			Assert.IsFalse (arr.IsGenericParameter, "#8");
1587 			Assert.IsFalse (arr.IsGenericType, "#9");
1588 			Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1589 			Assert.IsTrue (arr is TypeInfo, "#11");
1590 		}
1591 
1592 		[Test]
ArrayAsGenericArgumentOfNonSreType()1593 		public void ArrayAsGenericArgumentOfNonSreType ()
1594 		{
1595 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1596 
1597 			Type arr = tb.MakeArrayType ();
1598 			Type inst = typeof (Foo<>).MakeGenericType (arr);
1599 
1600 			MethodBuilder mb = tb.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (object), Type.EmptyTypes);
1601 
1602 			ILGenerator ilgen = mb.GetILGenerator ();
1603 			ilgen.Emit (OpCodes.Ldtoken, inst);
1604 			ilgen.Emit (OpCodes.Call, typeof (Type).GetMethod ("GetTypeFromHandle"));
1605 			ilgen.Emit (OpCodes.Ret);
1606 
1607 			Type res = tb.CreateType ();
1608 			Type expected = typeof (Foo<>).MakeGenericType (res.MakeArrayType ());
1609 
1610 			Assert.AreEqual (expected, res.GetMethod ("Main").Invoke (null, null), "#1");
1611 			Assert.IsNotNull (Activator.CreateInstance (expected), "#2");
1612 		}
1613 
1614 
1615 		[Test]
GenericTypeMembersOfGenericTypeParam()1616 		public void GenericTypeMembersOfGenericTypeParam ()
1617 		{
1618 			TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1619 			var gparam = tb.DefineGenericParameters ("F")[0];
1620 			Type arr = gparam.MakeArrayType ();
1621 
1622 			try {
1623 				arr.GetGenericArguments ();
1624 				Assert.Fail ("#1");
1625 			} catch (NotSupportedException) {}
1626 
1627 			try {
1628 				arr.GetGenericParameterConstraints ();
1629 				Assert.Fail ("#2");
1630 			} catch (InvalidOperationException) {}
1631 
1632 			try {
1633 				arr.GetGenericTypeDefinition ();
1634 				Assert.Fail ("#3");
1635 			} catch (NotSupportedException) {}
1636 
1637 			Assert.IsTrue (arr.ContainsGenericParameters, "#4");
1638 			try {
1639 				var x = arr.GenericParameterAttributes;
1640 				Assert.Fail ("#5");
1641 			} catch (NotSupportedException) {}
1642 
1643 			try {
1644 				var x = arr.GenericParameterPosition;
1645 				Assert.Fail ("#6");
1646 			} catch (InvalidOperationException) {}
1647 
1648 
1649 			Assert.IsFalse (arr.IsGenericParameter, "#8");
1650 			Assert.IsFalse (arr.IsGenericType, "#9");
1651 			Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1652 
1653 			Assert.AreEqual (TypeAttributes.Public, arr.Attributes, "#11");
1654 
1655 			Assert.IsTrue (arr.HasElementType, "#12");
1656 			Assert.IsTrue (arr.IsArray, "#13");
1657 
1658 			Assert.AreEqual (assembly, arr.Assembly, "#14");
1659 			Assert.AreEqual (null, arr.AssemblyQualifiedName, "#15");
1660 			Assert.AreEqual (typeof (Array), arr.BaseType, "#16");
1661 			Assert.AreEqual (null, arr.FullName, "#17");
1662 			Assert.AreEqual (module, arr.Module, "#18");
1663 			Assert.AreEqual (null, arr.Namespace, "#19");
1664 			Assert.AreEqual (arr, arr.UnderlyingSystemType, "#20");
1665 			Assert.AreEqual ("F[]", arr.Name, "#21");
1666 
1667 			Assert.AreEqual (gparam, arr.GetElementType (), "#22");
1668 			Assert.IsTrue (arr is TypeInfo, "#23");
1669 		}
1670 
1671 		[Test]
GenericTypeMembersOfEnum()1672 		public void GenericTypeMembersOfEnum ()
1673 		{
1674 			var eb = module.DefineEnum ("dd.enum", TypeAttributes.Public, typeof (int));
1675 			Type arr = eb.MakeArrayType ();
1676 
1677 			try {
1678 				arr.GetGenericArguments ();
1679 				Assert.Fail ("#1");
1680 			} catch (NotSupportedException) {}
1681 
1682 			try {
1683 				arr.GetGenericParameterConstraints ();
1684 				Assert.Fail ("#2");
1685 			} catch (InvalidOperationException) {}
1686 
1687 			try {
1688 				arr.GetGenericTypeDefinition ();
1689 				Assert.Fail ("#3");
1690 			} catch (NotSupportedException) {}
1691 
1692 			Assert.IsFalse (arr.ContainsGenericParameters, "#4");
1693 			try {
1694 				var x = arr.GenericParameterAttributes;
1695 				Assert.Fail ("#5");
1696 			} catch (NotSupportedException) {}
1697 
1698 			try {
1699 				var x = arr.GenericParameterPosition;
1700 				Assert.Fail ("#6");
1701 			} catch (InvalidOperationException) {}
1702 
1703 
1704 			Assert.IsFalse (arr.IsGenericParameter, "#8");
1705 			Assert.IsFalse (arr.IsGenericType, "#9");
1706 			Assert.IsFalse (arr.IsGenericTypeDefinition, "#10");
1707 
1708 			Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Sealed, arr.Attributes, "#11");
1709 
1710 			Assert.IsTrue (arr.HasElementType, "#12");
1711 			Assert.IsTrue (arr.IsArray, "#13");
1712 
1713 			Assert.AreEqual (assembly, arr.Assembly, "#14");
1714 			Assert.AreEqual ("dd.enum[], MonoTests.System.Reflection.Emit.TypeBuilderTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", arr.AssemblyQualifiedName, "#15");
1715 			Assert.AreEqual (typeof (Array), arr.BaseType, "#16");
1716 			Assert.AreEqual ("dd.enum[]", arr.FullName, "#17");
1717 			Assert.AreEqual (module, arr.Module, "#18");
1718 			Assert.AreEqual ("dd", arr.Namespace, "#19");
1719 			Assert.AreEqual (arr, arr.UnderlyingSystemType, "#20");
1720 			Assert.AreEqual ("enum[]", arr.Name, "#21");
1721 
1722 			Assert.AreEqual (eb, arr.GetElementType (), "#22");
1723 			Assert.IsTrue (arr is TypeInfo, "#23");
1724 		}
1725 
1726 	}
1727 }
1728