2 // DerivedType.cs - NUnit Test Cases for types derived from TypeBuilder
4 // Rodrigo Kumpera <rkumpera@novell.com>
6 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
10 using System.Collections;
11 using System.Threading;
12 using System.Reflection;
13 using System.Reflection.Emit;
15 using System.Security;
16 using System.Security.Permissions;
17 using System.Runtime.InteropServices;
18 using NUnit.Framework;
19 using System.Runtime.CompilerServices;
22 using System.Collections.Generic;
25 namespace MonoTests.System.Reflection.Emit
29 public class PointerTypeTest
31 AssemblyBuilder assembly;
34 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
38 return "internal__type"+ typeCount++;
42 protected void SetUp ()
44 AssemblyName assemblyName = new AssemblyName ();
45 assemblyName.Name = ASSEMBLY_NAME;
48 Thread.GetDomain ().DefineDynamicAssembly (
49 assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
51 module = assembly.DefineDynamicModule ("module1");
56 [Category ("NotDotNet")]
57 public void NonStandardPropertiesValues ()
60 Those are tests for properties that return non-sense values on MS.
62 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
63 Type ptr = tb.MakePointerType ();
64 //a ptr doesn't have any base type
65 Assert.AreEqual (null, ptr.BaseType, "#1");
69 public void PropertiesValue ()
71 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
72 Type ptr = tb.MakePointerType ();
74 Assert.AreEqual (assembly, ptr.Assembly, "#1");
75 Assert.AreEqual ("ns.type*, " + assembly.FullName, ptr.AssemblyQualifiedName, "#2");
76 Assert.AreEqual ("ns.type*", ptr.FullName, "#4");
77 Assert.AreEqual (module, ptr.Module, "#5");
78 Assert.AreEqual ("ns", ptr.Namespace, "#6");
79 Assert.AreEqual (ptr, ptr.UnderlyingSystemType, "#7");
80 Assert.AreEqual ("type*", ptr.Name, "#8");
85 } catch (NotSupportedException) {}
88 object x = ptr.TypeHandle;
90 } catch (NotSupportedException) {}
94 public void Methods ()
96 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
97 Type ptr = tb.MakePointerType ();
100 ptr.GetInterface ("foo", true);
102 } catch (NotSupportedException) {
107 ptr.GetInterfaces ();
109 } catch (NotSupportedException) {
113 Assert.AreEqual (tb, ptr.GetElementType ());
116 ptr.GetEvent ("foo", BindingFlags.Public);
118 } catch (NotSupportedException) {
123 ptr.GetEvents (BindingFlags.Public);
125 } catch (NotSupportedException) {
130 ptr.GetField ("foo", BindingFlags.Public);
132 } catch (NotSupportedException) {
137 ptr.GetFields (BindingFlags.Public);
139 } catch (NotSupportedException) {
144 ptr.GetMembers (BindingFlags.Public);
146 } catch (NotSupportedException) {
151 ptr.GetMethod ("Sort");
153 } catch (NotSupportedException) {
158 ptr.GetMethods (BindingFlags.Public);
160 } catch (NotSupportedException) {
165 ptr.GetNestedType ("bla", BindingFlags.Public);
167 } catch (NotSupportedException) {
172 ptr.GetNestedTypes (BindingFlags.Public);
174 } catch (NotSupportedException) {
179 ptr.GetProperties (BindingFlags.Public);
181 } catch (NotSupportedException) {
186 ptr.GetProperty ("Length");
188 } catch (NotSupportedException) {
193 ptr.GetConstructor (new Type[] { typeof (int) });
195 } catch (NotSupportedException) {
199 TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
200 Assert.AreEqual (attr, ptr.Attributes, "#15");
202 Assert.IsTrue (ptr.HasElementType, "#16");
203 Assert.IsFalse (ptr.IsArray, "#17");
204 Assert.IsFalse (ptr.IsByRef, "#18");
205 Assert.IsFalse (ptr.IsCOMObject, "#19");
206 Assert.IsTrue (ptr.IsPointer, "#20");
207 Assert.IsFalse (ptr.IsPrimitive, "#21");
210 ptr.GetConstructors (BindingFlags.Public);
212 } catch (NotSupportedException) {
217 ptr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
219 } catch (NotSupportedException) {
226 } catch (NotSupportedException) {
232 public void AttributeValues ()
234 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
235 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
237 tb = module.DefineType (MakeName (), TypeAttributes.Public);
238 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
240 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
241 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
243 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
244 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
248 public void AsParamType ()
251 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
252 Type ptr = tb.MakePointerType ();
254 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { ptr });
255 ILGenerator ilgen = mb.GetILGenerator ();
256 ilgen.Emit (OpCodes.Ret);
258 Type res = tb.CreateType ();
260 object o = Activator.CreateInstance (res);
261 //FIXME this crashes the runtime
262 //res.GetMethod ("Test").Invoke (o, new object[1] { null });
266 public void AsLocalVariable ()
269 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
270 Type ptr = tb.MakePointerType ();
272 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
273 ILGenerator ilgen = mb.GetILGenerator ();
274 ilgen.DeclareLocal (ptr);
275 ilgen.Emit (OpCodes.Ret);
277 Type res = tb.CreateType ();
279 object o = Activator.CreateInstance (res);
280 res.GetMethod ("Test").Invoke (o, null);
284 public void TestEquals ()
286 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
287 Type ptr = tb.MakePointerType ();
288 Type ptr2 = tb.MakePointerType ();
289 Assert.IsFalse (ptr.Equals (ptr2), "#1");
290 Assert.IsTrue (ptr.Equals (ptr), "#2");
294 public void IsSubclassOf ()
296 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
297 Type ptr = tb.MakePointerType ();
298 Assert.IsFalse (ptr.IsSubclassOf (tb), "#1");
299 Assert.IsFalse (ptr.IsSubclassOf (typeof (object[])), "#2");
303 public void IsAssignableFrom ()
305 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
306 Type ptr = tb.MakePointerType ();
307 Assert.IsFalse (ptr.IsAssignableFrom (tb), "#1");
308 Assert.IsFalse (ptr.IsAssignableFrom (typeof (object[])), "#2");
309 Assert.IsFalse (typeof (object[]).IsAssignableFrom (ptr), "#3");
310 Assert.IsFalse (typeof (object).IsAssignableFrom (ptr), "#4");
314 public void GetInterfaceMap ()
316 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
317 Type ptr = tb.MakePointerType ();
319 ptr.GetInterfaceMap (typeof (IEnumerable));
321 } catch (NotSupportedException) {
327 public void IsInstanceOfType ()
329 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
330 Type ptr = tb.MakePointerType ();
331 Assert.IsFalse (ptr.IsInstanceOfType (tb), "#1");
332 Assert.IsFalse (ptr.IsInstanceOfType (null), "#2");
333 Assert.IsFalse (ptr.IsInstanceOfType (new object [1]), "#3");
335 Type t = tb.CreateType ();
336 object obj = Activator.CreateInstance (t);
337 Assert.IsFalse (ptr.IsInstanceOfType (obj), "#4");
341 public void IsGenericTypeDefinition ()
343 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
344 Type ptr = tb.MakePointerType ();
345 Assert.IsFalse (ptr.IsGenericTypeDefinition, "#1");
349 public void IsGenericType ()
351 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
352 Type ptr = tb.MakePointerType ();
353 Assert.IsFalse (ptr.IsGenericType, "#1");
357 public void MakeGenericType ()
359 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
360 Type ptr = tb.MakePointerType ();
362 ptr.MakeGenericType (new Type[] { typeof (string) });
364 } catch (NotSupportedException) {}
368 public void GenericParameterPosition ()
370 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
371 Type ptr = tb.MakePointerType ();
373 int pos = ptr.GenericParameterPosition;
375 } catch (InvalidOperationException) {}
379 public void GenericParameterAttributes ()
381 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
382 Type ptr = tb.MakePointerType ();
384 object attr = ptr.GenericParameterAttributes;
386 } catch (NotSupportedException) {}
390 public void GetGenericParameterConstraints ()
392 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
393 Type ptr = tb.MakePointerType ();
395 ptr.GetGenericParameterConstraints ();
397 } catch (InvalidOperationException) {}
401 public void MakeArrayType ()
403 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
404 Type ptr = tb.MakePointerType ();
405 Type res = ptr.MakeArrayType ();
406 Assert.IsNotNull (res, "#1");
407 Assert.IsTrue (res.IsArray, "#2");
409 res = ptr.MakeArrayType (2);
410 Assert.IsNotNull (res, "#3");
411 Assert.IsTrue (res.IsArray, "#4");
415 public void MakeByRefType ()
417 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
418 Type ptr = tb.MakePointerType ();
419 Type res = ptr.MakeByRefType ();
421 Assert.IsNotNull (res, "#1");
422 Assert.IsTrue (res.IsByRef, "#2");
426 public void MakePointerType ()
428 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
429 Type ptr = tb.MakePointerType ();
430 Type res = ptr.MakePointerType ();
432 Assert.IsNotNull (res, "#1");
433 Assert.IsTrue (res.IsPointer, "#2");
437 public void StructLayoutAttribute ()
439 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
440 Type ptr = tb.MakePointerType ();
442 object x = ptr.StructLayoutAttribute;
444 } catch (NotSupportedException) {}
450 public class ByrefTypeTest
452 AssemblyBuilder assembly;
453 ModuleBuilder module;
455 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
459 return "internal__type"+ typeCount++;
463 protected void SetUp ()
465 AssemblyName assemblyName = new AssemblyName ();
466 assemblyName.Name = ASSEMBLY_NAME;
469 Thread.GetDomain ().DefineDynamicAssembly (
470 assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
472 module = assembly.DefineDynamicModule ("module1");
477 [Category ("NotDotNet")]
478 public void NonStandardPropertiesValues ()
481 Those are tests for properties that return non-sense values on MS.
483 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
484 Type byref = tb.MakeByRefType ();
485 //a byref doesn't have any base type
486 Assert.AreEqual (null, byref.BaseType, "#1");
490 public void PropertiesValue ()
492 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
493 Type byref = tb.MakeByRefType ();
495 Assert.AreEqual (assembly, byref.Assembly, "#1");
496 Assert.AreEqual ("ns.type&, " + assembly.FullName, byref.AssemblyQualifiedName, "#2");
497 Assert.AreEqual ("ns.type&", byref.FullName, "#4");
498 Assert.AreEqual (module, byref.Module, "#5");
499 Assert.AreEqual ("ns", byref.Namespace, "#6");
500 Assert.AreEqual (byref, byref.UnderlyingSystemType, "#7");
501 Assert.AreEqual ("type&", byref.Name, "#8");
504 object x = byref.GUID;
506 } catch (NotSupportedException) {}
509 object x = byref.TypeHandle;
511 } catch (NotSupportedException) {}
515 public void Methods ()
517 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
518 Type byref = tb.MakeByRefType ();
521 byref.GetInterface ("foo", true);
523 } catch (NotSupportedException) {
528 byref.GetInterfaces ();
530 } catch (NotSupportedException) {
534 Assert.AreEqual (tb, byref.GetElementType ());
537 byref.GetEvent ("foo", BindingFlags.Public);
539 } catch (NotSupportedException) {
544 byref.GetEvents (BindingFlags.Public);
546 } catch (NotSupportedException) {
551 byref.GetField ("foo", BindingFlags.Public);
553 } catch (NotSupportedException) {
558 byref.GetFields (BindingFlags.Public);
560 } catch (NotSupportedException) {
565 byref.GetMembers (BindingFlags.Public);
567 } catch (NotSupportedException) {
572 byref.GetMethod ("Sort");
574 } catch (NotSupportedException) {
579 byref.GetMethods (BindingFlags.Public);
581 } catch (NotSupportedException) {
586 byref.GetNestedType ("bla", BindingFlags.Public);
588 } catch (NotSupportedException) {
593 byref.GetNestedTypes (BindingFlags.Public);
595 } catch (NotSupportedException) {
600 byref.GetProperties (BindingFlags.Public);
602 } catch (NotSupportedException) {
607 byref.GetProperty ("Length");
609 } catch (NotSupportedException) {
614 byref.GetConstructor (new Type[] { typeof (int) });
616 } catch (NotSupportedException) {
620 TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
621 Assert.AreEqual (attr, byref.Attributes, "#15");
623 Assert.IsTrue (byref.HasElementType, "#16");
624 Assert.IsFalse (byref.IsArray, "#17");
625 Assert.IsTrue (byref.IsByRef, "#18");
626 Assert.IsFalse (byref.IsCOMObject, "#19");
627 Assert.IsFalse (byref.IsPointer, "#20");
628 Assert.IsFalse (byref.IsPrimitive, "#21");
631 byref.GetConstructors (BindingFlags.Public);
633 } catch (NotSupportedException) {
638 byref.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
640 } catch (NotSupportedException) {
645 byref.GetArrayRank ();
647 } catch (NotSupportedException) {
653 public void AttributeValues ()
655 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
656 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
658 tb = module.DefineType (MakeName (), TypeAttributes.Public);
659 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
661 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
662 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
664 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
665 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
669 public void AsParamType ()
672 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
673 Type byref = tb.MakeByRefType ();
675 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { byref });
676 ILGenerator ilgen = mb.GetILGenerator ();
677 ilgen.Emit (OpCodes.Ret);
679 Type res = tb.CreateType ();
681 object o = Activator.CreateInstance (res);
682 res.GetMethod ("Test").Invoke (o, new object[1] { null });
686 public void AsLocalVariable ()
689 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
690 Type byref = tb.MakeByRefType ();
692 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [0]);
693 ILGenerator ilgen = mb.GetILGenerator ();
694 ilgen.DeclareLocal (byref);
695 ilgen.Emit (OpCodes.Ret);
697 Type res = tb.CreateType ();
699 object o = Activator.CreateInstance (res);
700 res.GetMethod ("Test").Invoke (o, null);
704 public void TestEquals ()
706 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
707 Type byref = tb.MakeByRefType ();
708 Type byref2 = tb.MakeByRefType ();
709 Assert.IsFalse (byref.Equals (byref2), "#1");
710 Assert.IsTrue (byref.Equals (byref), "#2");
714 public void IsSubclassOf ()
716 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
717 Type byref = tb.MakeByRefType ();
718 Assert.IsFalse (byref.IsSubclassOf (tb), "#1");
719 Assert.IsFalse (byref.IsSubclassOf (typeof (object[])), "#2");
723 public void IsAssignableFrom ()
725 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
726 Type byref = tb.MakeByRefType ();
727 Assert.IsFalse (byref.IsAssignableFrom (tb), "#1");
728 Assert.IsFalse (byref.IsAssignableFrom (typeof (object[])), "#2");
729 Assert.IsFalse (typeof (object[]).IsAssignableFrom (byref), "#3");
730 Assert.IsFalse (typeof (object).IsAssignableFrom (byref), "#4");
734 public void GetInterfaceMap ()
736 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
737 Type byref = tb.MakeByRefType ();
739 byref.GetInterfaceMap (typeof (IEnumerable));
741 } catch (NotSupportedException) {
747 public void IsInstanceOfType ()
749 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
750 Type byref = tb.MakeByRefType ();
751 Assert.IsFalse (byref.IsInstanceOfType (tb), "#1");
752 Assert.IsFalse (byref.IsInstanceOfType (null), "#2");
753 Assert.IsFalse (byref.IsInstanceOfType (new object [1]), "#3");
755 Type t = tb.CreateType ();
756 object obj = Activator.CreateInstance (t);
757 Assert.IsFalse (byref.IsInstanceOfType (obj), "#4");
761 public void IsGenericTypeDefinition ()
763 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
764 Type byref = tb.MakeByRefType ();
765 Assert.IsFalse (byref.IsGenericTypeDefinition, "#1");
769 public void IsGenericType ()
771 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
772 Type byref = tb.MakeByRefType ();
773 Assert.IsFalse (byref.IsGenericType, "#1");
777 public void MakeGenericType ()
779 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
780 Type byref = tb.MakeByRefType ();
782 byref.MakeGenericType (new Type[] { typeof (string) });
784 } catch (NotSupportedException) {}
788 public void GenericParameterPosition ()
790 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
791 Type byref = tb.MakeByRefType ();
793 int pos = byref.GenericParameterPosition;
795 } catch (InvalidOperationException) {}
799 public void GenericParameterAttributes ()
801 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
802 Type byref = tb.MakeByRefType ();
804 object attr = byref.GenericParameterAttributes;
806 } catch (NotSupportedException) {}
810 public void GetGenericParameterConstraints ()
812 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
813 Type byref = tb.MakeByRefType ();
815 byref.GetGenericParameterConstraints ();
817 } catch (InvalidOperationException) {}
821 public void MakeArrayType ()
823 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
824 Type byref = tb.MakeByRefType ();
826 byref.MakeArrayType ();
828 } catch (ArgumentException) {};
830 byref.MakeArrayType (2);
832 } catch (ArgumentException) {};
836 public void MakeByRefType ()
838 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
839 Type byref = tb.MakeByRefType ();
841 byref.MakeByRefType ();
843 } catch (ArgumentException) {}
847 public void MakePointerType ()
849 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
850 Type byref = tb.MakeByRefType ();
852 byref.MakePointerType ();
854 } catch (ArgumentException) {}
858 public void StructLayoutAttribute ()
860 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
861 Type byref = tb.MakeByRefType ();
863 object x = byref.StructLayoutAttribute;
865 } catch (NotSupportedException) {}
870 public class ArrayTypeTest
872 AssemblyBuilder assembly;
873 ModuleBuilder module;
875 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
879 return "internal__type"+ typeCount++;
883 protected void SetUp ()
885 AssemblyName assemblyName = new AssemblyName ();
886 assemblyName.Name = ASSEMBLY_NAME;
889 Thread.GetDomain ().DefineDynamicAssembly (
890 assemblyName, AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
892 module = assembly.DefineDynamicModule ("module1");
897 public void PropertiesValue ()
899 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
900 Type arr = tb.MakeArrayType ();
902 Assert.AreEqual (assembly, arr.Assembly, "#1");
903 Assert.AreEqual ("ns.type[], " + assembly.FullName, arr.AssemblyQualifiedName, "#2");
904 Assert.AreEqual (typeof (Array), arr.BaseType, "#3");
905 Assert.AreEqual ("ns.type[]", arr.FullName, "#4");
906 Assert.AreEqual (module, arr.Module, "#5");
907 Assert.AreEqual ("ns", arr.Namespace, "#6");
908 Assert.AreEqual (arr, arr.UnderlyingSystemType, "#7");
909 Assert.AreEqual ("type[]", arr.Name, "#8");
914 } catch (NotSupportedException) {}
917 object x = arr.TypeHandle;
919 } catch (NotSupportedException) {}
923 public void Methods ()
925 TypeBuilder tb = module.DefineType ("ns.type", TypeAttributes.Public);
926 Type arr = tb.MakeArrayType ();
929 arr.GetInterface ("foo", true);
931 } catch (NotSupportedException) {
936 arr.GetInterfaces ();
938 } catch (NotSupportedException) {
942 Assert.AreEqual (tb, arr.GetElementType ());
945 arr.GetEvent ("foo", BindingFlags.Public);
947 } catch (NotSupportedException) {
952 arr.GetEvents (BindingFlags.Public);
954 } catch (NotSupportedException) {
959 arr.GetField ("foo", BindingFlags.Public);
961 } catch (NotSupportedException) {
966 arr.GetFields (BindingFlags.Public);
968 } catch (NotSupportedException) {
973 arr.GetMembers (BindingFlags.Public);
975 } catch (NotSupportedException) {
980 arr.GetMethod ("Sort");
982 } catch (NotSupportedException) {
987 arr.GetMethods (BindingFlags.Public);
989 } catch (NotSupportedException) {
994 arr.GetNestedType ("bla", BindingFlags.Public);
996 } catch (NotSupportedException) {
1001 arr.GetNestedTypes (BindingFlags.Public);
1002 Assert.Fail ("#11");
1003 } catch (NotSupportedException) {
1008 arr.GetProperties (BindingFlags.Public);
1009 Assert.Fail ("#12");
1010 } catch (NotSupportedException) {
1015 arr.GetProperty ("Length");
1016 Assert.Fail ("#13");
1017 } catch (NotSupportedException) {
1022 arr.GetConstructor (new Type[] { typeof (int) });
1023 Assert.Fail ("#14");
1024 } catch (NotSupportedException) {
1028 TypeAttributes attr = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public;
1029 Assert.AreEqual (attr, arr.Attributes, "#15");
1031 Assert.IsTrue (arr.HasElementType, "#16");
1032 Assert.IsTrue (arr.IsArray, "#17");
1033 Assert.IsFalse (arr.IsByRef, "#18");
1034 Assert.IsFalse (arr.IsCOMObject, "#19");
1035 Assert.IsFalse (arr.IsPointer, "#20");
1036 Assert.IsFalse (arr.IsPrimitive, "#21");
1039 arr.GetConstructors (BindingFlags.Public);
1040 Assert.Fail ("#22");
1041 } catch (NotSupportedException) {
1046 arr.InvokeMember ("GetLength", BindingFlags.Public, null, null, null);
1047 Assert.Fail ("#23");
1048 } catch (NotSupportedException) {
1054 public void AttributeValues ()
1056 TypeBuilder tb = module.DefineType (MakeName (), TypeAttributes.NotPublic);
1057 Assert.AreEqual (TypeAttributes.NotPublic, tb.Attributes, "#1");
1059 tb = module.DefineType (MakeName (), TypeAttributes.Public);
1060 Assert.AreEqual (TypeAttributes.Public, tb.Attributes, "#2");
1062 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed);
1063 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Serializable | TypeAttributes.Sealed, tb.Attributes, "#3");
1065 tb = module.DefineType (MakeName (), TypeAttributes.Public | TypeAttributes.Abstract);
1066 Assert.AreEqual (TypeAttributes.Public | TypeAttributes.Abstract, tb.Attributes, "$4");
1070 public void AsReturnType ()
1073 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1074 Type arr = tb.MakeArrayType ();
1076 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1077 ILGenerator ilgen = mb.GetILGenerator ();
1078 ilgen.Emit (OpCodes.Ldnull);
1079 ilgen.Emit (OpCodes.Ret);
1081 Type res = tb.CreateType ();
1083 object o = Activator.CreateInstance (res);
1084 res.GetMethod ("Test").Invoke (o, null);
1088 public void AsParamType ()
1091 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1092 Type arr = tb.MakeArrayType ();
1094 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, typeof (void), new Type [1] { arr });
1095 ILGenerator ilgen = mb.GetILGenerator ();
1096 ilgen.Emit (OpCodes.Ret);
1098 Type res = tb.CreateType ();
1100 object o = Activator.CreateInstance (res);
1101 res.GetMethod ("Test").Invoke (o, new object[1] { null });
1105 public void AsLocalVariable ()
1108 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1109 Type arr = tb.MakeArrayType ();
1111 MethodBuilder mb = tb.DefineMethod ("Test", MethodAttributes.Public, arr, new Type [0]);
1112 ILGenerator ilgen = mb.GetILGenerator ();
1113 ilgen.DeclareLocal (arr);
1114 ilgen.Emit (OpCodes.Ldnull);
1115 ilgen.Emit (OpCodes.Stloc_0);
1116 ilgen.Emit (OpCodes.Ldloc_0);
1117 ilgen.Emit (OpCodes.Ret);
1119 Type res = tb.CreateType ();
1121 object o = Activator.CreateInstance (res);
1122 res.GetMethod ("Test").Invoke (o, null);
1126 public void TestEquals ()
1128 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1129 Type arr = tb.MakeArrayType ();
1130 Type arr2 = tb.MakeArrayType ();
1131 Assert.IsFalse (arr.Equals (arr2), "#1");
1132 Assert.IsTrue (arr.Equals (arr), "#2");
1136 public void IsSubclassOf ()
1138 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1139 Type arr = tb.MakeArrayType ();
1140 Assert.IsFalse (arr.IsSubclassOf (tb), "#1");
1141 Assert.IsFalse (arr.IsSubclassOf (typeof (object[])), "#2");
1145 public void IsAssignableFrom ()
1147 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1148 Type arr = tb.MakeArrayType ();
1149 Assert.IsFalse (arr.IsAssignableFrom (tb), "#1");
1150 Assert.IsFalse (arr.IsAssignableFrom (typeof (object[])), "#2");
1151 Assert.IsFalse (typeof (object[]).IsAssignableFrom (arr), "#3");
1152 Assert.IsFalse (typeof (object).IsAssignableFrom (arr), "#4");
1156 public void GetInterfaceMap ()
1158 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1159 Type arr = tb.MakeArrayType ();
1161 arr.GetInterfaceMap (typeof (IEnumerable));
1163 } catch (NotSupportedException) {
1169 public void IsInstanceOfType ()
1171 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1172 Type arr = tb.MakeArrayType ();
1173 Assert.IsFalse (arr.IsInstanceOfType (tb), "#1");
1174 Assert.IsFalse (arr.IsInstanceOfType (null), "#2");
1175 Assert.IsFalse (arr.IsInstanceOfType (new object [1]), "#3");
1177 Type t = tb.CreateType ();
1178 object obj = Array.CreateInstance (t, 10);
1179 Assert.IsFalse (arr.IsInstanceOfType (obj), "#4");
1183 public void IsGenericTypeDefinition ()
1185 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1186 Type arr = tb.MakeArrayType ();
1187 Assert.IsFalse (arr.IsGenericTypeDefinition, "#1");
1191 public void IsGenericType ()
1193 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1194 Type arr = tb.MakeArrayType ();
1195 Assert.IsFalse (arr.IsGenericType, "#1");
1199 public void MakeGenericType ()
1201 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1202 Type arr = tb.MakeArrayType ();
1204 arr.MakeGenericType (new Type[] { typeof (string) });
1206 } catch (NotSupportedException) {}
1210 public void GenericParameterPosition ()
1212 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1213 Type arr = tb.MakeArrayType ();
1215 int pos = arr.GenericParameterPosition;
1217 } catch (InvalidOperationException) {}
1221 public void GenericParameterAttributes ()
1223 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1224 Type arr = tb.MakeArrayType ();
1226 object attr = arr.GenericParameterAttributes;
1228 } catch (NotSupportedException) {}
1232 public void GetGenericParameterConstraints ()
1234 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1235 Type arr = tb.MakeArrayType ();
1237 arr.GetGenericParameterConstraints ();
1239 } catch (InvalidOperationException) {}
1243 public void MakeArrayType ()
1245 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1246 Type arr = tb.MakeArrayType ();
1247 Type res = arr.MakeArrayType ();
1248 Assert.IsNotNull (res, "#1");
1249 Assert.IsTrue (res.IsArray, "#2");
1251 res = arr.MakeArrayType (2);
1252 Assert.IsNotNull (res, "#3");
1253 Assert.IsTrue (res.IsArray, "#4");
1257 public void MakeByRefType ()
1259 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1260 Type arr = tb.MakeArrayType ();
1261 Type res = arr.MakeByRefType ();
1263 Assert.IsNotNull (res, "#1");
1264 Assert.IsTrue (res.IsByRef, "#2");
1268 public void MakePointerType ()
1270 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1271 Type arr = tb.MakeArrayType ();
1272 Type res = arr.MakePointerType ();
1274 Assert.IsNotNull (res, "#1");
1275 Assert.IsTrue (res.IsPointer, "#2");
1279 public void StructLayoutAttribute ()
1281 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1282 Type arr = tb.MakeArrayType ();
1284 object x = arr.StructLayoutAttribute;
1286 } catch (NotSupportedException) {}
1290 public void GetArrayRank ()
1292 TypeBuilder tb = module.DefineType ("dd.test", TypeAttributes.Public);
1293 Type arr = tb.MakeArrayType ();
1295 Assert.AreEqual (1, tb.MakeArrayType ().GetArrayRank (), "#1");
1296 Assert.AreEqual (2, tb.MakeArrayType (2).GetArrayRank (), "#2");